2022-03-21 21:09:59 +00:00
|
|
|
import { later, run, _cancelTimers as cancelTimers } from '@ember/runloop';
|
2018-09-25 16:28:26 +00:00
|
|
|
import { resolve } from 'rsvp';
|
|
|
|
import EmberObject from '@ember/object';
|
|
|
|
import Service from '@ember/service';
|
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import { render, settled } from '@ember/test-helpers';
|
2018-07-19 01:59:04 +00:00
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
import { create } from 'ember-cli-page-object';
|
|
|
|
import editForm from 'vault/tests/pages/components/edit-form';
|
|
|
|
|
|
|
|
const component = create(editForm);
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
const flash = Service.extend({
|
2018-07-19 01:59:04 +00:00
|
|
|
success: sinon.stub(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const createModel = (canDelete = true) => {
|
2018-09-25 16:28:26 +00:00
|
|
|
return EmberObject.create({
|
2021-12-17 03:44:29 +00:00
|
|
|
fields: [
|
|
|
|
{ name: 'one', type: 'string' },
|
|
|
|
{ name: 'two', type: 'boolean' },
|
|
|
|
],
|
2018-07-19 01:59:04 +00:00
|
|
|
canDelete,
|
|
|
|
destroyRecord() {
|
2018-09-25 16:28:26 +00:00
|
|
|
return resolve();
|
2018-07-19 01:59:04 +00:00
|
|
|
},
|
|
|
|
save() {
|
2018-09-25 16:28:26 +00:00
|
|
|
return resolve();
|
2018-07-19 01:59:04 +00:00
|
|
|
},
|
|
|
|
rollbackAttributes() {},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
module('Integration | Component | edit form', function (hooks) {
|
2018-09-25 16:28:26 +00:00
|
|
|
setupRenderingTest(hooks);
|
2018-07-19 01:59:04 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
hooks.beforeEach(function () {
|
2018-09-25 16:28:26 +00:00
|
|
|
run(() => {
|
|
|
|
this.owner.unregister('service:flash-messages');
|
|
|
|
this.owner.register('service:flash-messages', flash);
|
|
|
|
});
|
|
|
|
});
|
2018-07-19 01:59:04 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it renders', async function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let model = createModel();
|
|
|
|
this.set('model', model);
|
|
|
|
await render(hbs`{{edit-form model=model}}`);
|
2018-07-19 01:59:04 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.ok(component.fields.length, 2);
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it calls flash message fns on save', async function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let model = createModel();
|
|
|
|
let onSave = () => {
|
|
|
|
return resolve();
|
|
|
|
};
|
|
|
|
this.set('model', model);
|
|
|
|
this.set('onSave', onSave);
|
|
|
|
let saveSpy = sinon.spy(this, 'onSave');
|
|
|
|
|
|
|
|
await render(hbs`{{edit-form model=model onSave=onSave}}`);
|
|
|
|
|
|
|
|
component.submit();
|
2022-03-21 21:09:59 +00:00
|
|
|
later(() => cancelTimers(), 50);
|
2018-09-25 16:28:26 +00:00
|
|
|
return settled().then(() => {
|
|
|
|
assert.ok(saveSpy.calledOnce, 'calls passed onSave');
|
|
|
|
assert.equal(saveSpy.getCall(0).args[0].saveType, 'save');
|
|
|
|
assert.deepEqual(saveSpy.getCall(0).args[0].model, model, 'passes model to onSave');
|
|
|
|
let flash = this.owner.lookup('service:flash-messages');
|
|
|
|
assert.equal(flash.success.callCount, 1, 'calls flash message success');
|
|
|
|
});
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|
|
|
|
});
|