From 6688a9bff28eb4e54f8092d6855c8a072db846dd Mon Sep 17 00:00:00 2001 From: Joel Thoms Date: Fri, 28 Sep 2018 10:19:06 -0700 Subject: [PATCH 1/2] setTimeout should be mockable. --- internal/timeout.js | 6 ------ threading/__tests__/sleep.test.js | 23 ++++++++--------------- threading/sleep.js | 4 +--- 3 files changed, 9 insertions(+), 24 deletions(-) delete mode 100644 internal/timeout.js diff --git a/internal/timeout.js b/internal/timeout.js deleted file mode 100644 index 75ee507..0000000 --- a/internal/timeout.js +++ /dev/null @@ -1,6 +0,0 @@ -/* eslint-disable */ -const curry2 = require('./curry2') - -const timeout = curry2 (setTimeout) - -module.exports = timeout diff --git a/threading/__tests__/sleep.test.js b/threading/__tests__/sleep.test.js index 4d75589..0954a7c 100644 --- a/threading/__tests__/sleep.test.js +++ b/threading/__tests__/sleep.test.js @@ -1,6 +1,9 @@ const sleep = require('../sleep') describe('threading/sleep', () => { + beforeEach(() => jest.spyOn(global, 'setTimeout').mockImplementation(func => func())) + afterEach(() => global.setTimeout.mockReset()) + test('sleep returns value', () => { expect.assertions(1) const expected = 888 @@ -8,21 +11,11 @@ describe('threading/sleep', () => { return expect(actual).resolves.toBe(expected) }) - test('sleep(0) executes immediately', () => { - expect.assertions(1) - const start = new Date() - return sleep(0)().then(() => { - const end = new Date() - expect(end - start).toBeLessThan(100) - }) - }) - - test('sleep(100) executes later', () => { + test('sleep(100000) calls setTimeout with 100000 milliseconds', () => { expect.assertions(1) - const start = new Date() - return sleep(100)().then(() => { - const end = new Date() - expect(end - start).toBeGreaterThanOrEqual(99) - }) + const expected = 1000 + sleep(expected)() + const actual = global.setTimeout.mock.calls[0][1] + expect(actual).toBe(expected) }) }) diff --git a/threading/sleep.js b/threading/sleep.js index 94b61ca..d199298 100644 --- a/threading/sleep.js +++ b/threading/sleep.js @@ -1,8 +1,6 @@ /* eslint-disable */ -const timeout = require('../internal/timeout') - const sleep = milliseconds => value => new Promise ( - resolve => timeout (() => resolve (value)) (milliseconds) + resolve => setTimeout(() => resolve(value), milliseconds) ) module.exports = sleep From 68b0d2a7cf36eaee37fe27fc3432c1f591603448 Mon Sep 17 00:00:00 2001 From: Joel Thoms Date: Fri, 28 Sep 2018 10:27:42 -0700 Subject: [PATCH 2/2] allow axios to be mocked --- net/axios.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/net/axios.js b/net/axios.js index bac9f21..608dd40 100644 --- a/net/axios.js +++ b/net/axios.js @@ -1,14 +1,12 @@ /* eslint-disable */ -const curry2 = require('../internal/curry2') -const curry3 = require('../internal/curry3') const axios = require('axios') module.exports = { - get: curry2 (axios.get), - 'delete': curry2 (axios.delete), - head: curry2 (axios.head), - options: curry2 (axios.options), - post: curry3 (axios.post), - put: curry3 (axios.put), - patch: curry3 (axios.put) + get: url => options => axios.get(url, options), + 'delete': url => options => axios.delete(url, options), + head: url => options => axios.head(url, options), + options: url => options => axios.options(url, options), + post: url => data => options => axios.post(url, data, options), + put: url => data => options => axios.put(url, data, options), + patch: url => data => options => axios.put(url, data, options) }