Skip to content

Migrate the frontend to use the new yank API #9765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/adapters/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ApplicationAdapter from './application';

export default class VersionAdapter extends ApplicationAdapter {
urlForUpdateRecord(id, modelName, snapshot) {
let crateName = snapshot.record.crate.id;
let num = snapshot.record.num;
return `/${this.namespace}/crates/${crateName}/${num}`;
}
}
22 changes: 8 additions & 14 deletions app/models/version.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import { waitForPromise } from '@ember/test-waiters';

import { apiAction } from '@mainmatter/ember-api-actions';
import { keepLatestTask, task } from 'ember-concurrency';
import fetch from 'fetch';
import { alias } from 'macro-decorators';
Expand Down Expand Up @@ -166,22 +168,14 @@ export default class Version extends Model {
}

yankTask = keepLatestTask(async () => {
let response = await fetch(`/api/v1/crates/${this.crate.id}/${this.num}/yank`, { method: 'DELETE' });
if (!response.ok) {
throw new Error(`Yank request for ${this.crateName} v${this.num} failed`);
}
this.set('yanked', true);

return await response.text();
let data = { version: { yanked: true } };
let payload = await waitForPromise(apiAction(this, { method: 'PATCH', data }));
this.store.pushPayload(payload);
});

unyankTask = keepLatestTask(async () => {
let response = await fetch(`/api/v1/crates/${this.crate.id}/${this.num}/unyank`, { method: 'PUT' });
if (!response.ok) {
throw new Error(`Unyank request for ${this.crateName} v${this.num} failed`);
}
this.set('yanked', false);

return await response.text();
let data = { version: { yanked: false } };
let payload = await waitForPromise(apiAction(this, { method: 'PATCH', data }));
this.store.pushPayload(payload);
});
}
14 changes: 14 additions & 0 deletions e2e/acceptance/sudo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,23 @@ test.describe('Acceptance | sudo', { tag: '@acceptance' }, () => {

await yankButton.click();

// Verify backend state after yanking
const yankedVersion = await page.evaluate(() => {
const crate = server.schema['crates'].findBy({ name: 'foo' });
return server.schema['versions'].findBy({ crateId: crate.id, num: '0.1.0', yanked: true });
});
expect(yankedVersion, 'The version should be yanked').toBeTruthy();

await expect(unyankButton).toBeVisible();
await unyankButton.click();

// Verify backend state after unyanking
const unyankedVersion = await page.evaluate(() => {
const crate = server.schema['crates'].findBy({ name: 'foo' });
return server.schema['versions'].findBy({ crateId: crate.id, num: '0.1.0', yanked: false });
});
expect(unyankedVersion, 'The version should be unyanked').toBeTruthy();

await expect(yankButton).toBeVisible();
});
});
5 changes: 5 additions & 0 deletions tests/acceptance/sudo-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ module('Acceptance | sudo', function (hooks) {
await click('[data-test-version-yank-button="0.1.0"]');

await waitFor('[data-test-version-unyank-button="0.1.0"]');
const crate = this.server.schema.crates.findBy({ name: 'foo' });
const version = this.server.schema.versions.findBy({ crateId: crate.id, num: '0.1.0' });
assert.true(version.yanked, 'The version should be yanked');
assert.dom('[data-test-version-unyank-button="0.1.0"]').exists();
await click('[data-test-version-unyank-button="0.1.0"]');
const updatedVersion = this.server.schema.versions.findBy({ crateId: crate.id, num: '0.1.0' });
assert.false(updatedVersion.yanked, 'The version should be unyanked');

await waitFor('[data-test-version-yank-button="0.1.0"]');
assert.dom('[data-test-version-yank-button="0.1.0"]').exists();
Expand Down
Loading