Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smikhalevski committed Nov 12, 2023
1 parent ded6f61 commit 82fe8a7
Showing 1 changed file with 80 additions and 111 deletions.
191 changes: 80 additions & 111 deletions packages/uncontrolled-plugin/src/test/uncontrolledPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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();
});
});
});

0 comments on commit 82fe8a7

Please sign in to comment.