From 82fe8a776be420635924317cb2654789c25eab3d Mon Sep 17 00:00:00 2001 From: smikhalevski Date: Mon, 13 Nov 2023 00:01:43 +0300 Subject: [PATCH] Fixed tests --- .../src/test/uncontrolledPlugin.test.ts | 191 ++++++++---------- 1 file changed, 80 insertions(+), 111 deletions(-) diff --git a/packages/uncontrolled-plugin/src/test/uncontrolledPlugin.test.ts b/packages/uncontrolled-plugin/src/test/uncontrolledPlugin.test.ts index d2ac27c..1dadb2c 100644 --- a/packages/uncontrolled-plugin/src/test/uncontrolledPlugin.test.ts +++ b/packages/uncontrolled-plugin/src/test/uncontrolledPlugin.test.ts @@ -66,6 +66,86 @@ describe('uncontrolledPlugin', () => { expect(refMock).toHaveBeenNthCalledWith(1, element); }); + test('does not call setValue if the same value multiple times', () => { + const accessorMock: ElementsValueAccessor = { + get: jest.fn(() => 'xxx'), + set: jest.fn(), + }; + + const field = createField('aaa', uncontrolledPlugin(accessorMock)); + + const setValueSpy = jest.spyOn(field, 'setValue'); + + field.ref(element); + + expect(accessorMock.set).toHaveBeenCalledTimes(1); + expect(accessorMock.set).toHaveBeenNthCalledWith(1, [element], 'aaa'); + expect(accessorMock.get).not.toHaveBeenCalled(); + expect(setValueSpy).not.toHaveBeenCalled(); + + fireEvent.change(element, { target: { value: 'bbb' } }); + fireEvent.input(element, { target: { value: 'bbb' } }); + + expect(setValueSpy).toHaveBeenCalledTimes(1); + expect(setValueSpy).toHaveBeenNthCalledWith(1, 'xxx'); + + expect(accessorMock.set).toHaveBeenCalledTimes(1); + }); + + test('uses accessor to set values to the element', () => { + const accessorMock: ElementsValueAccessor = { + get: () => undefined, + set: jest.fn(), + }; + + const field = createField({ aaa: 111 }, uncontrolledPlugin(accessorMock)); + + field.at('aaa').ref(element); + + expect(accessorMock.set).toHaveBeenCalledTimes(1); + expect(accessorMock.set).toHaveBeenNthCalledWith(1, [element], 111); + + field.at('aaa').setValue(222); + + expect(accessorMock.set).toHaveBeenCalledTimes(2); + expect(accessorMock.set).toHaveBeenNthCalledWith(2, [element], 222); + }); + + test('does not call set accessor if there are no referenced elements', () => { + const accessorMock: ElementsValueAccessor = { + get: () => undefined, + set: jest.fn(), + }; + + const field = createField({ aaa: 111 }, uncontrolledPlugin(accessorMock)); + + field.at('aaa').setValue(222); + + expect(accessorMock.set).not.toHaveBeenCalled(); + }); + + test('multiple elements are passed to set accessor', () => { + const accessorMock: ElementsValueAccessor = { + get: () => 'xxx', + set: jest.fn(), + }; + + const element1 = document.body.appendChild(document.createElement('input')); + const element2 = document.body.appendChild(document.createElement('textarea')); + + const field = createField({ aaa: 111 }, uncontrolledPlugin(accessorMock)); + + field.at('aaa').refFor(1)(element1); + + expect(accessorMock.set).toHaveBeenCalledTimes(1); + expect(accessorMock.set).toHaveBeenNthCalledWith(1, [element1], 111); + + field.at('aaa').refFor(2)(element2); + + expect(accessorMock.set).toHaveBeenCalledTimes(2); + expect(accessorMock.set).toHaveBeenNthCalledWith(2, [element1, element2], 111); + }); + // test('does not invoke preceding plugin if an additional element is added', () => { // const refMock = jest.fn(); // const plugin = (field: any) => { @@ -142,115 +222,4 @@ describe('uncontrolledPlugin', () => { // // expect(refMock).not.toHaveBeenCalled(); // }); - - test('does not call setValue if the same value multiple times', () => { - const elementsValueAccessorMock: ElementsValueAccessor = { - get: jest.fn(() => 'xxx'), - set: jest.fn(), - }; - - const field = createField('aaa', uncontrolledPlugin(elementsValueAccessorMock)); - - const setValueSpy = jest.spyOn(field, 'setValue'); - - field.ref(element); - - expect(elementsValueAccessorMock.set).toHaveBeenCalledTimes(1); - expect(elementsValueAccessorMock.set).toHaveBeenNthCalledWith(1, [element], 'aaa'); - expect(elementsValueAccessorMock.get).not.toHaveBeenCalled(); - expect(setValueSpy).not.toHaveBeenCalled(); - - fireEvent.change(element, { target: { value: 'bbb' } }); - fireEvent.input(element, { target: { value: 'bbb' } }); - - expect(setValueSpy).toHaveBeenCalledTimes(1); - expect(setValueSpy).toHaveBeenNthCalledWith(1, 'xxx'); - - expect(elementsValueAccessorMock.set).toHaveBeenCalledTimes(2); - expect(elementsValueAccessorMock.set).toHaveBeenNthCalledWith(2, [element], 'xxx'); - }); - - test('uses accessor to set values to the element', () => { - const accessorMock: ElementsValueAccessor = { - get: () => undefined, - set: jest.fn(), - }; - - const field = createField({ aaa: 111 }, uncontrolledPlugin(accessorMock)); - - field.at('aaa').ref(element); - - expect(accessorMock.set).toHaveBeenCalledTimes(1); - expect(accessorMock.set).toHaveBeenNthCalledWith(1, [element], 111); - - field.at('aaa').setValue(222); - - expect(accessorMock.set).toHaveBeenCalledTimes(2); - expect(accessorMock.set).toHaveBeenNthCalledWith(2, [element], 222); - }); - - test('does not call set accessor if there are no referenced elements', () => { - const accessorMock: ElementsValueAccessor = { - get: () => undefined, - set: jest.fn(), - }; - - const field = createField({ aaa: 111 }, uncontrolledPlugin(accessorMock)); - - field.at('aaa').setValue(222); - - expect(accessorMock.set).not.toHaveBeenCalled(); - }); - - test('multiple elements are passed to set accessor', () => { - const accessorMock: ElementsValueAccessor = { - get: () => 'xxx', - set: jest.fn(), - }; - - const element1 = document.body.appendChild(document.createElement('input')); - const element2 = document.body.appendChild(document.createElement('textarea')); - - const field = createField({ aaa: 111 }, uncontrolledPlugin(accessorMock)); - - field.at('aaa').ref(element1); - - expect(accessorMock.set).toHaveBeenCalledTimes(1); - expect(accessorMock.set).toHaveBeenNthCalledWith(1, [element1], 111); - - field.at('aaa').ref(element2); - - expect(accessorMock.set).toHaveBeenCalledTimes(2); - expect(accessorMock.set).toHaveBeenNthCalledWith(2, [element1, element2], 111); - }); - - test('non-connected elements are ignored', () => { - const accessorMock: ElementsValueAccessor = { - get: () => 'xxx', - set: jest.fn(), - }; - - const element = document.createElement('input'); - - const field = createField({ aaa: 111 }, uncontrolledPlugin(accessorMock)); - - field.at('aaa').ref(element); - - expect(accessorMock.set).toHaveBeenCalledTimes(0); - }); - - test('mutation refr disconnects after last element is removed', done => { - const disconnectMock = jest.spyOn(MutationObserver.prototype, 'disconnect'); - - const field = createField({ aaa: 111 }, uncontrolledPlugin()); - - field.at('aaa').ref(element); - - element.remove(); - - queueMicrotask(() => { - expect(disconnectMock).toHaveBeenCalledTimes(1); - done(); - }); - }); });