|
| 1 | +import '@testing-library/jest-dom' |
| 2 | +import { html } from 'lit-html' |
| 3 | +import { screen } from "testing-library__dom"; |
| 4 | +import userEvent from "@testing-library/user-event" |
| 5 | +import { fixture } from "@open-wc/testing-helpers"; |
| 6 | +import './vmd-commune-or-departement-selector.component' |
| 7 | + |
| 8 | +import { Commune, Departement } from '../state/State' |
| 9 | +import { delay } from '../utils/Schedulers' |
| 10 | + |
| 11 | +const départementCalvados: Departement = { |
| 12 | + code_departement: '14', |
| 13 | + nom_departement: 'Calvados', |
| 14 | + code_region: 28, |
| 15 | + nom_region: 'Normandie' |
| 16 | +} |
| 17 | + |
| 18 | +const communeDeauville: Commune = { |
| 19 | + code: "14220", |
| 20 | + codePostal: "14800", |
| 21 | + nom: "Deauville", |
| 22 | + codeDepartement: "14", |
| 23 | + longitude: 0.0772, |
| 24 | + latitude: 49.3531 |
| 25 | +} |
| 26 | + |
| 27 | +describe('<vmd-commune-or-departement-selector />', () => { |
| 28 | + let onCommuneSelected = jest.fn() |
| 29 | + let onDepartementSelected = jest.fn() |
| 30 | + let suggest = jest.fn(() => Promise.resolve([] as any[])) |
| 31 | + const placeholder = "Commune, Code postal, Département..." |
| 32 | + beforeEach(() => { |
| 33 | + onCommuneSelected.mockClear() |
| 34 | + onDepartementSelected.mockClear() |
| 35 | + suggest.mockClear() |
| 36 | + window.scroll = jest.fn() |
| 37 | + suggest.mockImplementation(async () => ([ |
| 38 | + départementCalvados, |
| 39 | + communeDeauville |
| 40 | + ])) |
| 41 | + }) |
| 42 | + describe('when a value is given', () => { |
| 43 | + describe('and is a département', () => { |
| 44 | + beforeEach(async () => { |
| 45 | + await fixture(html` |
| 46 | + <vmd-commune-or-departement-selector |
| 47 | + @on-commune-selected="${onCommuneSelected}" |
| 48 | + @on-departement-selected="${onDepartementSelected}" |
| 49 | + .suggest="${suggest}" |
| 50 | + .value="${départementCalvados}" |
| 51 | + /> |
| 52 | + `) |
| 53 | + }) |
| 54 | + |
| 55 | + it('displays the name into the input', async () => { |
| 56 | + // Then |
| 57 | + const input = screen.getByPlaceholderText(placeholder) |
| 58 | + expect(input).toHaveValue('14 - Calvados') |
| 59 | + }) |
| 60 | + }) |
| 61 | + describe('and is a commune', () => { |
| 62 | + beforeEach(async () => { |
| 63 | + await fixture(html` |
| 64 | + <vmd-commune-or-departement-selector |
| 65 | + @on-commune-selected="${onCommuneSelected}" |
| 66 | + @on-departement-selected="${onDepartementSelected}" |
| 67 | + .suggest="${suggest}" |
| 68 | + .value="${communeDeauville}" |
| 69 | + /> |
| 70 | + `) |
| 71 | + }) |
| 72 | + |
| 73 | + it('displays the name into the input', async () => { |
| 74 | + // Then |
| 75 | + const input = screen.getByPlaceholderText(placeholder) |
| 76 | + expect(input).toHaveValue('14800 - Deauville') |
| 77 | + }) |
| 78 | + }) |
| 79 | + describe('and the focuses the input', () => { |
| 80 | + it('selects all the content', async () => { |
| 81 | + // Given |
| 82 | + await fixture(html` |
| 83 | + <vmd-commune-or-departement-selector |
| 84 | + @on-commune-selected="${onCommuneSelected}" |
| 85 | + @on-departement-selected="${onDepartementSelected}" |
| 86 | + .suggest="${suggest}" |
| 87 | + .value="${communeDeauville}" |
| 88 | + /> |
| 89 | + `) |
| 90 | + const input = screen.getByPlaceholderText(placeholder) as HTMLInputElement |
| 91 | + // When |
| 92 | + await userEvent.click(input) |
| 93 | + await cooldown() |
| 94 | + // Then |
| 95 | + const selectedText = input.value.substring(input.selectionStart!, input.selectionEnd!) |
| 96 | + expect(selectedText).toEqual(input.value) |
| 97 | + }) |
| 98 | + }) |
| 99 | + }) |
| 100 | + describe('when no value is given and something typed', () => { |
| 101 | + const value = undefined |
| 102 | + beforeEach(async () => { |
| 103 | + await fixture(html` |
| 104 | + <vmd-commune-or-departement-selector |
| 105 | + @on-commune-selected="${onCommuneSelected}" |
| 106 | + @on-departement-selected="${onDepartementSelected}" |
| 107 | + .suggest="${suggest}" |
| 108 | + .value="${value}" |
| 109 | + /> |
| 110 | + `) |
| 111 | + }) |
| 112 | + it('shows suggestions from suggest() attribute', async () => { |
| 113 | + // Given |
| 114 | + const input = screen.getByPlaceholderText(placeholder) |
| 115 | + // When |
| 116 | + await userEvent.type(input, 'Cal') |
| 117 | + // Then |
| 118 | + expect(suggest).toHaveBeenCalledWith('Cal') |
| 119 | + expect(await screen.findByRole('listbox')).toBeDefined() |
| 120 | + const suggestions = await screen.findAllByRole('option') |
| 121 | + expect(suggestions).toHaveLength(2) |
| 122 | + expect(suggestions[0]).toHaveTextContent('14 - Calvados') |
| 123 | + expect(suggestions[1]).toHaveTextContent('14800 - Deauville') |
| 124 | + }) |
| 125 | + |
| 126 | + describe('when pressing {enter}', () => { |
| 127 | + it('selects the first suggestion', async () => { |
| 128 | + // Given |
| 129 | + const input = screen.getByPlaceholderText(placeholder) |
| 130 | + // When |
| 131 | + await userEvent.type(input, 'Cal{enter}', { delay: 20 }) |
| 132 | + // Then |
| 133 | + expect(onDepartementSelected).toHaveBeenCalledTimes(1) |
| 134 | + }) |
| 135 | + describe('after pressing arrow down once', () => { |
| 136 | + it('selects the second suggestion', async () => { |
| 137 | + // Given |
| 138 | + const input = screen.getByPlaceholderText(placeholder) |
| 139 | + // When |
| 140 | + await userEvent.type(input, 'Cal{arrowdown}{enter}', { delay: 20 }) |
| 141 | + // Then |
| 142 | + expect(onCommuneSelected).toHaveBeenCalledTimes(1) |
| 143 | + }) |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + describe('when a suggestion is clicked', () => { |
| 148 | + describe('and is a département', () => { |
| 149 | + beforeEach(async () => { |
| 150 | + // Given |
| 151 | + const input = screen.getByPlaceholderText(placeholder) |
| 152 | + // When |
| 153 | + await userEvent.type(input, 'Cal') |
| 154 | + const [ suggestionCalvados ] = await screen.findAllByRole('option') |
| 155 | + await userEvent.click(suggestionCalvados) |
| 156 | + }) |
| 157 | + it("emits 'on-departement-selected'", async () => { |
| 158 | + // Then |
| 159 | + expect(onCommuneSelected).toHaveBeenCalledTimes(0) |
| 160 | + expect(onDepartementSelected).toHaveBeenCalledTimes(1) |
| 161 | + expect(onDepartementSelected.mock.calls[0][0].detail).toEqual({ |
| 162 | + departement: départementCalvados |
| 163 | + }) |
| 164 | + }) |
| 165 | + it("hides the suggestions", async () => { |
| 166 | + // Then |
| 167 | + expect(screen.queryByRole('listbox')).toEqual(null) |
| 168 | + expect(await screen.queryAllByRole('option')).toHaveLength(0) |
| 169 | + }) |
| 170 | + }) |
| 171 | + describe('and is a commune', () => { |
| 172 | + beforeEach(async () => { |
| 173 | + // Given |
| 174 | + const input = screen.getByPlaceholderText(placeholder) |
| 175 | + // When |
| 176 | + await userEvent.type(input, 'Cal') |
| 177 | + const [ _, suggestionDeauville ] = await screen.findAllByRole('option') |
| 178 | + await userEvent.click(suggestionDeauville) |
| 179 | + }) |
| 180 | + it("emits 'on-commune-selected'", async () => { |
| 181 | + // Then |
| 182 | + expect(onCommuneSelected).toHaveBeenCalledTimes(1) |
| 183 | + expect(onDepartementSelected).toHaveBeenCalledTimes(0) |
| 184 | + expect(onCommuneSelected.mock.calls[0][0].detail).toEqual({ |
| 185 | + commune: communeDeauville |
| 186 | + }) |
| 187 | + }) |
| 188 | + it("hides the suggestions", async () => { |
| 189 | + // Then |
| 190 | + expect(screen.queryByRole('listbox')).toEqual(null) |
| 191 | + expect(await screen.queryAllByRole('option')).toHaveLength(0) |
| 192 | + }) |
| 193 | + }) |
| 194 | + }) |
| 195 | + }) |
| 196 | +}) |
| 197 | + |
| 198 | +const cooldown = () => delay(100) |
0 commit comments