47 lines
997 B
JavaScript
47 lines
997 B
JavaScript
import { backgroundTask } from '../src/index.js';
|
|
|
|
describe('A background task', () => {
|
|
it('initially does nothing.', done => {
|
|
const task = backgroundTask(() =>
|
|
done(new Error('Background tasks should not spontaneously execute.'))
|
|
);
|
|
expect().nothing();
|
|
setTimeout(done, 50);
|
|
});
|
|
|
|
it('runs a task (but not right away)', done => {
|
|
let shouldRun = false;
|
|
const task = backgroundTask(() => {
|
|
expect(shouldRun).toBeTruthy();
|
|
done();
|
|
});
|
|
|
|
task();
|
|
shouldRun = true;
|
|
setTimeout(done, 50);
|
|
});
|
|
|
|
it('passes params', done => {
|
|
let testVal = Math.random();
|
|
const task = backgroundTask(val => {
|
|
expect(val).toEqual(testVal);
|
|
done();
|
|
});
|
|
|
|
task(testVal);
|
|
setTimeout(done, 50);
|
|
});
|
|
|
|
it('queues multiple calls to be run sequentially', done => {
|
|
let testVal = ['a', 'd', 'q'];
|
|
const task = backgroundTask((val, index) => {
|
|
expect(val).toEqual(testVal[index]);
|
|
if (index === testVal.length - 1) {
|
|
done();
|
|
}
|
|
});
|
|
|
|
testVal.map(task);
|
|
});
|
|
});
|