Last active
January 5, 2018 19:03
-
-
Save mnoble01/4a21a4ec59000b0e45ecb012513ef423 to your computer and use it in GitHub Desktop.
Exploring async testing examples with ember-milestones (https://github.com/salsify/ember-milestones)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************************************** | |
** Basic timeout example | |
**/ | |
// component | |
export default Component.extend({ | |
pollStatus: task(function*() { | |
yield milestone('poll-status-timeout', () => timeout(1000)); | |
this.get('statusModel').reload(); | |
}), | |
}); | |
// component-test | |
test('it polls for status updates', async function() { | |
const milestones = activateMilestones(['poll-status-timeout']); | |
// ... | |
await milestones.advanceTo('poll-status-timeout').andPause(); | |
assert.equal(this.component.status, 'running'); | |
milestones.unpause().andReturn(); | |
// ... | |
milestones.deactivate(); | |
}); | |
/********************************************************************************** | |
** Component example w/ two milestones | |
**/ | |
// edit-approval component | |
export default Component.extend({ | |
async init() { | |
this._super(...arguments); | |
await milestone('load-changeset', async () => { | |
await this.get('loadChanges').perform(); | |
this.get('validators').revalidate(); | |
}); | |
}, | |
async _updateAllChanges(properties) { | |
const describable = this.get('changeset.targetItem'); | |
for (const change of this.get('changeset.proposedChanges')) { | |
const assignment = describable.assignmentForProperty(get(change, 'propertyId')); | |
setProperties(change, { | |
...properties, | |
baseLockVersion: get(assignment, 'lockVersion'), | |
}); | |
} | |
await milestone('save-changeset', async () => { | |
await this.get('saveChanges').perform(); | |
this.get('validators').revalidate(); | |
}); | |
}, | |
}) | |
// edit-approval-test | |
moduleForComponentPageObject('edit-approval', 'Integration | Component | edit approval', { | |
componentPageObject: 'edit-approval', | |
integration: true, | |
beforeEach() { | |
// ... | |
}, | |
afterEach() { | |
this.milestones.deactivate(); | |
}, | |
}); | |
test('it allows proposed changes to be approved', async function(assert) { | |
this.milestones = activateMilestones(['load-changeset', 'save-changeset']); | |
this.render(hbs` | |
{{task-ui-components/edit-approval | |
config=config | |
env=env | |
}} | |
`); | |
await this.milestones.advanceTo('load-changeset').andContinue(); | |
assert.notOk(this.get('env.validators').areAllValid()); | |
this.component.acceptButton.click(); | |
assert.ok(this.get('env.validators').areAllValid()); | |
await this.milestones.advanceTo('save-changeset').andContinue(); | |
this.inject.service('itemRepoStore'); | |
assert.ok(this.get('itemRepoStore.changeset').save.calledOnce); | |
assert.ok(this.get('itemRepoStore.changeset.proposedChanges').every((change) => { | |
return change.accepted === true; | |
})); | |
}); | |
/********************************************************************************** | |
** Notes | |
**/ | |
// If you wrap the whole thing, you can't test error handling | |
// so just wrap the part that can throw an error | |
loadData: task(function*() { | |
try { | |
yield milestone('load-data', async () => { | |
await this.get('loadTask').perform() | |
await this.get('loadPrincipal').perform() | |
}); | |
} catch (e) { | |
// error handling | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment