Skip to content

Instantly share code, notes, and snippets.

@yohanmishkin
Created April 12, 2019 20:32
Show Gist options
  • Save yohanmishkin/0f7d1a0c6e138738af672559bbba886c to your computer and use it in GitHub Desktop.
Save yohanmishkin/0f7d1a0c6e138738af672559bbba886c to your computer and use it in GitHub Desktop.
Testing polling in Ember
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]');
});
});
class pollingService {
constructor() {
this.isPolling = false;
}
start(thingWeWantToPoll, pollingFrequency) {
this.isPolling = true;
while (this.isPolling) {
yield thingWeWantToPoll();
yield timeout(pollingFrequency);
}
}
stop() {
this.isPolling = false;
}
}
class RoomRoute {
pollingService: service(),
activate() {
pollingService.start(this.fetchRooms.perform(), 5000);
}
deactivate() {
pollingService.stop();
}
}
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