From 6173c5e153d899fba4f1f3e4e2695e77845c67c1 Mon Sep 17 00:00:00 2001 From: Kir Belevich Date: Tue, 1 Aug 2017 13:40:38 +0200 Subject: [PATCH] :space_invader: debounce-handler: improve tests --- .../test/__snapshots__/index.jsx.snap | 26 ++++++++ packages/debounce-handler/test/index.jsx | 60 +++++++++---------- 2 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 packages/debounce-handler/test/__snapshots__/index.jsx.snap diff --git a/packages/debounce-handler/test/__snapshots__/index.jsx.snap b/packages/debounce-handler/test/__snapshots__/index.jsx.snap new file mode 100644 index 0000000..36be784 --- /dev/null +++ b/packages/debounce-handler/test/__snapshots__/index.jsx.snap @@ -0,0 +1,26 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`debounceHandler should debounce handler with \`delay\` option 1`] = ` +Array [ + Array [ + "e", + ], + Array [ + "f", + ], +] +`; + +exports[`debounceHandler should debounce handler with \`leadingCall\` option 1`] = ` +Array [ + Array [ + "a", + ], + Array [ + "e", + ], + Array [ + "f", + ], +] +`; diff --git a/packages/debounce-handler/test/index.jsx b/packages/debounce-handler/test/index.jsx index c68a50e..c079394 100644 --- a/packages/debounce-handler/test/index.jsx +++ b/packages/debounce-handler/test/index.jsx @@ -3,8 +3,6 @@ import { mount } from 'enzyme'; import debounceHandler from '../src/'; -const DELAY = 20; -const HALF_DELAY = DELAY / 2; const Target = () => null; describe('debounceHandler', () => { @@ -42,7 +40,7 @@ describe('debounceHandler', () => { }); it('should debounce handler with `delay` option', (done) => { - const EnchancedTarget = debounceHandler('testHandler', DELAY)(Target); + const EnchancedTarget = debounceHandler('testHandler', 50)(Target); const mockTestHandler = jest.fn(); const wrapper = mount( @@ -50,32 +48,35 @@ describe('debounceHandler', () => { const testHandler = wrapper.find(Target).prop('testHandler'); testHandler('a'); - expect(mockTestHandler).toHaveBeenCalledTimes(0); setTimeout(() => { testHandler('b'); - expect(mockTestHandler).toHaveBeenCalledTimes(0); setTimeout(() => { testHandler('c'); - expect(mockTestHandler).toHaveBeenCalledTimes(0); setTimeout(() => { testHandler('d'); - expect(mockTestHandler).toHaveBeenCalledTimes(0); setTimeout(() => { - expect(mockTestHandler).toHaveBeenCalledTimes(1); - expect(mockTestHandler).toHaveBeenCalledWith('d'); - done(); - }, DELAY); - }, HALF_DELAY); - }, HALF_DELAY); - }, HALF_DELAY); + testHandler('e'); + + setTimeout(() => { + testHandler('f'); + + setTimeout(() => { + expect(mockTestHandler.mock.calls).toMatchSnapshot(); + done(); + }, 50); + }, 50); + }, 40); + }, 30); + }, 30); + }, 30); }); it('should debounce handler with `leadingCall` option', (done) => { - const EnchancedTarget = debounceHandler('testHandler', DELAY, true)(Target); + const EnchancedTarget = debounceHandler('testHandler', 50, true)(Target); const mockTestHandler = jest.fn(); const wrapper = mount( @@ -83,32 +84,31 @@ describe('debounceHandler', () => { const testHandler = wrapper.find(Target).prop('testHandler'); testHandler('a'); - expect(mockTestHandler).toHaveBeenCalledTimes(1); - expect(mockTestHandler).toHaveBeenCalledWith('a'); setTimeout(() => { testHandler('b'); - expect(mockTestHandler).toHaveBeenCalledTimes(1); - expect(mockTestHandler).toHaveBeenCalledWith('a'); setTimeout(() => { testHandler('c'); - expect(mockTestHandler).toHaveBeenCalledTimes(1); - expect(mockTestHandler).toHaveBeenCalledWith('a'); setTimeout(() => { testHandler('d'); - expect(mockTestHandler).toHaveBeenCalledTimes(1); - expect(mockTestHandler).toHaveBeenCalledWith('a'); setTimeout(() => { - expect(mockTestHandler).toHaveBeenCalledTimes(2); - expect(mockTestHandler).toHaveBeenCalledWith('d'); - done(); - }, DELAY); - }, HALF_DELAY); - }, HALF_DELAY); - }, HALF_DELAY); + testHandler('e'); + + setTimeout(() => { + testHandler('f'); + + setTimeout(() => { + expect(mockTestHandler.mock.calls).toMatchSnapshot(); + done(); + }, 50); + }, 50); + }, 20); + }, 20); + }, 20); + }, 20); }); describe('display name', () => {