Skip to content

Commit

Permalink
Refactor tests to use host interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Jan 19, 2018
1 parent 79b3956 commit 78b8fb1
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 77 deletions.
143 changes: 82 additions & 61 deletions tests/configurable-timeout-test.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,81 @@
import Backburner from 'backburner';
import Backburner, { buildDefaultPlatform, IPlatform } from 'backburner';

QUnit.module('tests/configurable-timeout');

QUnit.test('We can configure a custom platform', function(assert) {
assert.expect(1);

let fakePlatform = {
setTimeout() {},
clearTimeout() {},
isFakePlatform: true
};

let bb = new Backburner(['one'], {
_platform: fakePlatform
_buildPlatform(flush) {
let platform = buildDefaultPlatform(flush);
platform['isFakePlatform'] = true;
return platform;
}
});

assert.ok(bb.options._platform.isFakePlatform, 'We can pass in a custom platform');
assert.ok(bb['_platform']!['isFakePlatform'], 'We can pass in a custom platform');
});

QUnit.test('We can use a custom setTimeout', function(assert) {
assert.expect(2);
assert.expect(1);
let done = assert.async();

let customNextWasUsed = false;
let bb = new Backburner(['one'], {
_platform: {
next() {
throw new TypeError('NOT IMPLEMENTED');
},
setTimeout(cb) {
customNextWasUsed = true;
return setTimeout(cb);
},
clearTimeout(timer) {
return clearTimeout(timer);
},
isFakePlatform: true
_buildPlatform(flush) {
return {
next() {
throw new TypeError('NOT IMPLEMENTED');
},
clearNext() { },
setTimeout(cb) {
customNextWasUsed = true;
return setTimeout(cb);
},
clearTimeout(timer) {
return clearTimeout(timer);
},
now() {
return Date.now();
},
isFakePlatform: true
};
}
});

bb.setTimeout(() => {
assert.ok(bb.options._platform.isFakePlatform, 'we are using the fake platform');
assert.ok(customNextWasUsed , 'custom later was used');
done();
});
});

QUnit.test('We can use a custom next', function(assert) {
assert.expect(2);
assert.expect(1);
let done = assert.async();

let customNextWasUsed = false;
let bb = new Backburner(['one'], {
_platform: {
setTimeout() {
throw new TypeError('NOT IMPLEMENTED');
},
next(cb) {
// next is used for the autorun
customNextWasUsed = true;
return setTimeout(cb);
},
clearTimeout(timer) {
return clearTimeout(timer);
},
isFakePlatform: true
_buildPlatform(flush) {
return {
setTimeout() {
throw new TypeError('NOT IMPLEMENTED');
},
clearTimeout(timer) {
return clearTimeout(timer);
},
next() {
// next is used for the autorun
customNextWasUsed = true;
return setTimeout(flush);
},
clearNext() { },
now() { return Date.now(); },
isFakePlatform: true
};
}
});

bb.scheduleOnce('one', () => {
assert.ok(bb.options._platform.isFakePlatform, 'we are using the fake platform');
assert.ok(customNextWasUsed , 'custom later was used');
done();
});
Expand All @@ -81,21 +87,26 @@ QUnit.test('We can use a custom clearTimeout', function(assert) {
let functionWasCalled = false;
let customClearTimeoutWasUsed = false;
let bb = new Backburner(['one'], {
_platform: {
setTimeout(method, wait) {
return setTimeout(method, wait);
},
clearTimeout(timer) {
customClearTimeoutWasUsed = true;
return clearTimeout(timer);
},
next(method) {
return setTimeout(method, 0);
},
clearNext(timer) {
customClearTimeoutWasUsed = true;
return clearTimeout(timer);
}
_buildPlatform(flush) {
return {
setTimeout(method, wait) {
return setTimeout(method, wait);
},
clearTimeout(timer) {
customClearTimeoutWasUsed = true;
return clearTimeout(timer);
},
next() {
return setTimeout(flush, 0);
},
clearNext(timer) {
customClearTimeoutWasUsed = true;
return clearTimeout(timer);
},
now() {
return Date.now();
}
};
}
});

Expand All @@ -111,23 +122,33 @@ QUnit.test('We can use a custom clearTimeout', function(assert) {
});

QUnit.test('We can use a custom now', function(assert) {
assert.expect(2);
assert.expect(1);
let done = assert.async();

let currentTime = 10;
let customNowWasUsed = false;
let bb = new Backburner(['one'], {
_platform: {
now() {
customNowWasUsed = true;
return currentTime += 10;
},
isFakePlatform: true
_buildPlatform(flush) {
return {
setTimeout(method, wait) {
return setTimeout(method, wait);
},
clearTimeout(id) {
clearTimeout(id);
},
next() {
return setTimeout(flush, 0);
},
clearNext() { },
now() {
customNowWasUsed = true;
return currentTime += 10;
},
};
}
});

bb.later(() => {
assert.ok(bb.options._platform.isFakePlatform, 'we are using the fake platform');
assert.ok(customNowWasUsed , 'custom now was used');
done();
}, 10);
Expand Down
27 changes: 11 additions & 16 deletions tests/multi-turn-test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import Backburner from 'backburner';
import Backburner, { buildDefaultPlatform } from 'backburner';

QUnit.module('tests/multi-turn');

const queue: any[] = [];
const platform = {
flushSync() {
let current = queue.slice();
queue.length = 0;
current.forEach((task) => task());
},

// TDB actually implement
next(cb) {
queue.push(cb);
}
};
let platform;
function buildFakePlatform(flush) {
platform = buildDefaultPlatform(flush);
platform.flushSync = function() {
flush();
};
return platform;
}

QUnit.test('basic', function(assert) {
let bb = new Backburner(['zomg'], {

// This is just a place holder for now, but somehow the system needs to
// know to when to stop
mustYield() {
return true; // yield after each step, for now.
},
_platform: platform
_buildPlatform: buildFakePlatform
});

let order = -1;
Expand Down Expand Up @@ -88,7 +83,7 @@ QUnit.test('properly cancel items which are added during flush', function(assert
return true; // yield after each step, for now.
},

_platform: platform
_buildPlatform: buildFakePlatform
});

let fooCalled = 0;
Expand Down

0 comments on commit 78b8fb1

Please sign in to comment.