|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +import { fireEvent, render } from '@testing-library/svelte'; |
| 5 | +import moment from 'moment/moment'; |
| 6 | +import DateEditorWrapper from './DateEditorWrapper.svelte'; |
| 7 | + |
| 8 | +import { getAndCheckRenderedElement } from './RenderingTestHelpers'; |
| 9 | + |
| 10 | +window.moment = moment; |
| 11 | + |
| 12 | +function renderDateEditorWrapper(componentOptions: { forwardOnly: boolean }) { |
| 13 | + const { container } = render(DateEditorWrapper, componentOptions); |
| 14 | + |
| 15 | + expect(() => container).toBeTruthy(); |
| 16 | + |
| 17 | + return container; |
| 18 | +} |
| 19 | + |
| 20 | +function testInputValue(container: HTMLElement, inputId: string, expectedText: string) { |
| 21 | + const input = getAndCheckRenderedElement<HTMLInputElement>(container, inputId); |
| 22 | + expect(input.value).toEqual(expectedText); |
| 23 | +} |
| 24 | + |
| 25 | +async function testTypingInput( |
| 26 | + { |
| 27 | + userTyped, |
| 28 | + expectedLeftText, |
| 29 | + expectedRightText, |
| 30 | + expectedReturnedDate, |
| 31 | + }: { |
| 32 | + userTyped: string; |
| 33 | + expectedLeftText: string; |
| 34 | + expectedRightText: string; |
| 35 | + expectedReturnedDate: string; |
| 36 | + }, |
| 37 | + { forwardOnly }: { forwardOnly: boolean } = { forwardOnly: true }, |
| 38 | +) { |
| 39 | + const container = renderDateEditorWrapper({ forwardOnly }); |
| 40 | + |
| 41 | + const dueDateInput = getAndCheckRenderedElement<HTMLInputElement>(container, 'due'); |
| 42 | + await fireEvent.input(dueDateInput, { target: { value: userTyped } }); |
| 43 | + |
| 44 | + testInputValue(container, 'due', expectedLeftText); |
| 45 | + testInputValue(container, 'parsedDateFromDateEditor', expectedRightText); |
| 46 | + testInputValue(container, 'dueDateFromDateEditor', expectedReturnedDate); |
| 47 | +} |
| 48 | + |
| 49 | +beforeEach(() => { |
| 50 | + jest.useFakeTimers(); |
| 51 | + jest.setSystemTime(new Date('2024-04-20')); |
| 52 | +}); |
| 53 | + |
| 54 | +afterEach(() => { |
| 55 | + jest.useRealTimers(); |
| 56 | +}); |
| 57 | + |
| 58 | +describe('date editor wrapper tests', () => { |
| 59 | + it('should initialise fields correctly', () => { |
| 60 | + const container = renderDateEditorWrapper({ forwardOnly: true }); |
| 61 | + |
| 62 | + testInputValue(container, 'due', ''); |
| 63 | + testInputValue(container, 'parsedDateFromDateEditor', '<i>no due date</i>'); |
| 64 | + testInputValue(container, 'dueDateFromDateEditor', ''); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should replace an empty date field with typed date value', async () => { |
| 68 | + await testTypingInput({ |
| 69 | + userTyped: '2024-10-01', |
| 70 | + expectedLeftText: '2024-10-01', |
| 71 | + expectedRightText: '2024-10-01', |
| 72 | + expectedReturnedDate: '2024-10-01', |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should replace an empty date field with typed abbreviation', async () => { |
| 77 | + await testTypingInput({ |
| 78 | + userTyped: 'tm ', |
| 79 | + expectedLeftText: 'tomorrow', |
| 80 | + expectedRightText: '2024-04-21', |
| 81 | + expectedReturnedDate: 'tomorrow', |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should show an error message for invalid date', async () => { |
| 86 | + await testTypingInput({ |
| 87 | + userTyped: 'blah', |
| 88 | + expectedLeftText: 'blah', |
| 89 | + expectedRightText: '<i>invalid due date</i>', |
| 90 | + expectedReturnedDate: 'blah', |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should select a forward date', async () => { |
| 95 | + await testTypingInput( |
| 96 | + { |
| 97 | + userTyped: 'friday', |
| 98 | + expectedLeftText: 'friday', |
| 99 | + expectedRightText: '2024-04-26', |
| 100 | + expectedReturnedDate: 'friday', |
| 101 | + }, |
| 102 | + { forwardOnly: true }, |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should select a backward/earlier date', async () => { |
| 107 | + await testTypingInput( |
| 108 | + { |
| 109 | + userTyped: 'friday', |
| 110 | + expectedLeftText: 'friday', |
| 111 | + expectedRightText: '2024-04-19', |
| 112 | + expectedReturnedDate: 'friday', |
| 113 | + }, |
| 114 | + { forwardOnly: false }, |
| 115 | + ); |
| 116 | + }); |
| 117 | +}); |
0 commit comments