Fixed publishmenu re-rending side-effects breaking tests

no issue
- we currently rely on a side-effect of the individual publish menu components calling `setSaveType` when they are rendered
- in tests sometimes the timing was not what we expected and the menu would swap components mid-save which then updated the save type and caused further problems
- added a guard so that we never swap the publish menu components whilst a save is in progress
This commit is contained in:
Kevin Ansfield 2018-03-27 18:06:55 +01:00
parent 38b138d759
commit d6169c38ef
5 changed files with 32 additions and 3 deletions

View File

@ -9,7 +9,9 @@ export default Component.extend({
clock: service(),
classNames: 'gh-publishmenu',
displayState: 'draft',
post: null,
postStatus: 'draft',
saveTask: null,
runningText: null,
@ -102,6 +104,28 @@ export default Component.extend({
return buttonText;
}),
didReceiveAttrs() {
this._super(...arguments);
// update the displayState based on the post status but only after a
// save has finished to avoid swapping the menu prematurely and triggering
// calls to `setSaveType` due to the component re-rendering
// TODO: we should have a better way of dealing with this where we don't
// rely on the side-effect of component rendering calling setSaveType
let postStatus = this.get('postStatus');
if (postStatus !== this._postStatus) {
if (this.get('saveTask.isRunning')) {
this.get('saveTask.last').then(() => {
this.set('displayState', postStatus);
});
} else {
this.set('displayState', postStatus);
}
}
this._postStatus = this.get('postStatus');
},
actions: {
setSaveType(saveType) {
let post = this.get('post');

View File

@ -4,12 +4,12 @@
{{/dd.trigger}}
{{#dd.content class="gh-publishmenu-dropdown"}}
{{#if (eq postState "published")}}
{{#if (eq displayState "published")}}
{{gh-publishmenu-published
saveType=saveType
setSaveType=(action "setSaveType")}}
{{else if (eq postState "scheduled")}}
{{else if (eq displayState "scheduled")}}
{{gh-publishmenu-scheduled
post=post
saveType=saveType

View File

@ -28,6 +28,7 @@
{{else}}
{{gh-publishmenu
post=post
postStatus=post.status
saveTask=save
setSaveType=(action "setSaveType")
onOpen=(action "cancelAutosave")}}

View File

@ -1,3 +1,4 @@
import moment from 'moment';
import {Response} from 'ember-cli-mirage';
import {dasherize} from '@ember/string';
import {isBlank} from '@ember/utils';
@ -74,6 +75,8 @@ export default function mockPosts(server) {
attrs.authors = authors;
attrs.updatedAt = moment.utc().toDate();
return post.update(attrs);
});

View File

@ -434,7 +434,7 @@ describe('Acceptance: Editor', function () {
});
});
let post = server.create('post', 1, {authors: [author]});
let post = server.create('post', 1, {authors: [author], status: 'draft'});
let plusTenMin = moment().utc().add(10, 'minutes');
await visit(`/editor/${post.id}`);
@ -444,6 +444,7 @@ describe('Acceptance: Editor', function () {
await datepickerSelect('[data-test-publishmenu-draft] [data-test-date-time-picker-datepicker]', plusTenMin);
await fillIn('[data-test-publishmenu-draft] [data-test-date-time-picker-time-input]', plusTenMin.format('HH:mm'));
await triggerEvent('[data-test-publishmenu-draft] [data-test-date-time-picker-time-input]', 'blur');
await click('[data-test-publishmenu-save]');
expect(