Created
April 12, 2019 20:32
-
-
Save yohanmishkin/0f7d1a0c6e138738af672559bbba886c to your computer and use it in GitHub Desktop.
Testing polling in Ember
This file contains hidden or 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
module('Acceptance | Rooms', () => { | |
test('Room page polls for room data', async function(assert) { | |
assert.expect(2); | |
let pollingServiceMock = { | |
start(fetchingTaskInstance, pollingFrequency) { | |
assert.ok(fetchingTaskInstance, 'Polling started for rooms'); | |
assert.equal(pollingFrequency, 5000, 'Poll every 5 seconds'); | |
} | |
}; | |
this.owner.register('service:polling-service', pollingServiceMock); | |
await visit('/rooms/1') | |
}); | |
test('Room page stops polling when you leave the page', async function(assert) { | |
assert.expect(2); | |
let pollingServiceMock = { | |
start() { | |
assert.ok(true, 'Polling started'); | |
} | |
stop() { | |
assert.ok(true, 'Polling stopped'); | |
} | |
}; | |
this.owner.register('service:polling-service', pollingServiceMock); | |
await visit('/rooms/1') | |
await click('[data-test-menu-entries]'); | |
}); | |
}); |
This file contains hidden or 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
class pollingService { | |
constructor() { | |
this.isPolling = false; | |
} | |
start(thingWeWantToPoll, pollingFrequency) { | |
this.isPolling = true; | |
while (this.isPolling) { | |
yield thingWeWantToPoll(); | |
yield timeout(pollingFrequency); | |
} | |
} | |
stop() { | |
this.isPolling = false; | |
} | |
} |
This file contains hidden or 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
class RoomRoute { | |
pollingService: service(), | |
activate() { | |
pollingService.start(this.fetchRooms.perform(), 5000); | |
} | |
deactivate() { | |
pollingService.stop(); | |
} | |
} |
This file contains hidden or 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
module('Unit | Polling Service', () => { | |
test('Polling service starts a task', async function(assert) { | |
assert.expect(2); | |
let functionToPoll = () => { | |
assert.ok(true, 'function called'); | |
}; | |
let pollingService = this.owner.lookup('service:metrics'); | |
pollingService.start(functionToPoll, 0); | |
expect.ok(pollingService.isPolling); | |
}); | |
test('Polling service stops a task', async function(assert) { | |
assert.expect(1); | |
let pollingService = this.owner.lookup('service:metrics'); | |
pollingService.stop(); | |
expect.notOk(pollingService.isPolling); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment