Add updateRecord to role adapter (#18993)

* add updateRecord to role adapter to correctly handle the query when the the is not new.

* wip

* update and cancel test

* clean up

* wip

* final

* clean up

* split test in two

* clean up
This commit is contained in:
Angel Garbarino 2023-02-07 08:53:40 -07:00 committed by GitHub
parent 5d1869d6fe
commit 4e160284b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 4 deletions

View File

@ -34,6 +34,13 @@ export default class PkiRoleAdapter extends ApplicationAdapter {
});
}
updateRecord(store, type, snapshot) {
const { name, backend } = snapshot.record;
const data = this.serialize(snapshot);
const url = this._urlForRole(backend, name);
return this.ajax(url, 'POST', { data });
}
fetchByQuery(store, query) {
const { id, backend } = query;
@ -55,6 +62,7 @@ export default class PkiRoleAdapter extends ApplicationAdapter {
queryRecord(store, type, query) {
return this.fetchByQuery(store, query);
}
deleteRecord(store, type, snapshot) {
const { id, record } = snapshot;
return this.ajax(this._urlForRole(record.backend, id), 'DELETE');

View File

@ -111,8 +111,50 @@ module('Integration | Component | pki-role-form', function (hooks) {
await click(SELECTORS.roleCreateButton);
});
/* FUTURE TEST TODO:
* it should update role
* it should unload the record on cancel
*/
test('it should unload model on cancel', async function (assert) {
assert.expect(3);
this.onCancel = () => assert.ok(true, 'onCancel callback fires');
await render(
hbs`
<PkiRoleForm
@model={{this.model}}
@onCancel={{this.onCancel}}
@onSave={{this.onSave}}
/>
`,
{ owner: this.engine }
);
await fillIn(SELECTORS.roleName, 'dont-save-me');
await click(SELECTORS.roleCancelButton);
assert.notEqual(this.model.roleName, 'dont-save-me');
assert.true(this.model.isDestroyed, 'new model is unloaded on cancel');
});
test('it should update attributes on the model on update', async function (assert) {
assert.expect(1);
this.store.pushPayload('pki/role', {
modelName: 'pki/role',
name: 'test-role',
backend: 'pki-test',
id: 'role-id',
});
this.model = this.store.peekRecord('pki/role', 'role-id');
await render(
hbs`
<PkiRoleForm
@model={{this.model}}
@onCancel={{this.onCancel}}
@onSave={{this.onSave}}
/>
`,
{ owner: this.engine }
);
await fillIn(SELECTORS.issuerRef, 'not-default');
await click(SELECTORS.roleCreateButton);
assert.strictEqual(this.model.issuerRef, 'not-default', 'Issuer Ref correctly saved on create');
});
});

View File

@ -0,0 +1,29 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
module('Unit | Adapter | pki/role', function (hooks) {
setupTest(hooks);
setupMirage(hooks);
hooks.beforeEach(function () {
this.store = this.owner.lookup('service:store');
this.store.unloadAll('pki/role');
});
test('it should make request to correct endpoint when updating a record', async function (assert) {
assert.expect(1);
this.server.post('/pki-not-hardcoded/roles/pki-test', () => {
assert.ok(true, 'POST request made to correct endpoint when updating a record');
});
this.store.pushPayload('pki/role', {
modelName: 'pki/role',
backend: 'pki-not-hardcoded',
id: 'pki-test',
name: 'pki-test',
});
const record = this.store.peekRecord('pki/role', 'pki-test');
await record.save();
});
});