|
5 | 5 | * file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
6 | 6 | */
|
7 | 7 |
|
8 |
| -import Calculator from './calculator' |
| 8 | +import Calculator from './calculator.js' |
| 9 | +import Template from './calculator.hbs' |
9 | 10 | import { afterAll, afterEach, describe, expect, test, vi } from 'vitest'
|
10 |
| -import StringCalculatorPage from '../test/page' |
| 11 | +import StringCalculatorPage from '../test/page.js' |
11 | 12 |
|
12 | 13 | // @vitest-environment jsdom
|
13 | 14 | describe('Calculator', () => {
|
14 | 15 | const page = StringCalculatorPage.new()
|
15 | 16 |
|
16 | 17 | const setup = () => {
|
17 | 18 | const postFormData = vi.fn()
|
| 19 | + /** @type {import('./calculators.js').StrCalcDescriptors} */ |
18 | 20 | const calculators = {
|
19 | 21 | 'api': { label: 'API', impl: postFormData },
|
20 |
| - 'browser': { label: 'Browser', impl: () => {} } |
| 22 | + 'browser': { label: 'Browser', impl: vi.fn() } |
21 | 23 | }
|
22 | 24 |
|
23 | 25 | new Calculator().init({ appElem: page.appElem, calculators })
|
24 | 26 | return { page, postFormData }
|
25 | 27 | }
|
26 | 28 |
|
| 29 | + const setupConsoleErrorSpy = () => { |
| 30 | + const consoleSpy = vi.spyOn(console, 'error') |
| 31 | + .mockImplementationOnce(() => {}) |
| 32 | + |
| 33 | + return { |
| 34 | + consoleSpy, |
| 35 | + loggedError: () => { |
| 36 | + const lastCall = consoleSpy.mock.lastCall |
| 37 | + if (!lastCall) throw new Error('error not logged') |
| 38 | + return lastCall |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param {string} numbersString - input to the StringCalculator |
| 45 | + * @returns {FormData} - form data to submit to the implementation |
| 46 | + */ |
27 | 47 | const expectedFormData = (numbersString) => {
|
28 | 48 | const data = new FormData()
|
29 | 49 | data.append('numbers', numbersString)
|
30 | 50 | return data
|
31 | 51 | }
|
32 | 52 |
|
33 |
| - afterEach(() => page.clear()) |
| 53 | + afterEach(() => { |
| 54 | + vi.restoreAllMocks() |
| 55 | + page.clear() |
| 56 | + }) |
| 57 | + |
34 | 58 | afterAll(() => page.remove())
|
35 | 59 |
|
36 | 60 | test('renders form and result placeholder', async () => {
|
@@ -59,4 +83,51 @@ describe('Calculator', () => {
|
59 | 83 | await expect(result).resolves.toBe('Error: D\'oh!')
|
60 | 84 | expect(postFormData).toHaveBeenCalledWith(expectedFormData('2,2'))
|
61 | 85 | })
|
| 86 | + |
| 87 | + test('logs error if missing numbers input element', async () => { |
| 88 | + const { loggedError } = setupConsoleErrorSpy() |
| 89 | + /** @type {import('./calculators.js').StrCalcDescriptors} */ |
| 90 | + const calculators = {} |
| 91 | + /** |
| 92 | + * @param {any} context - init parameters for template |
| 93 | + * @returns {DocumentFragment} - template elements without #numbers element |
| 94 | + */ |
| 95 | + const BadTemplate = (context) => { |
| 96 | + const t = Template({ context }) |
| 97 | + const [ form ] = t.children |
| 98 | + const input = form.querySelector('#numbers') |
| 99 | + |
| 100 | + if (input !== null) input.remove() |
| 101 | + return t |
| 102 | + } |
| 103 | + |
| 104 | + new Calculator().init( |
| 105 | + { appElem: page.appElem, calculators, instantiate: BadTemplate } |
| 106 | + ) |
| 107 | + |
| 108 | + expect(await vi.waitFor(loggedError)) |
| 109 | + .toStrictEqual(['missing numbers input']) |
| 110 | + }) |
| 111 | + |
| 112 | + test('logs error if missing implementation input element', async () => { |
| 113 | + const { page } = setup() |
| 114 | + const { loggedError } = setupConsoleErrorSpy() |
| 115 | + |
| 116 | + page.impl().remove() |
| 117 | + page.enterValueAndSubmit('2,2') |
| 118 | + |
| 119 | + expect(await vi.waitFor(loggedError)) |
| 120 | + .toStrictEqual(['missing implementation input']) |
| 121 | + }) |
| 122 | + |
| 123 | + test('logs error if missing result element', async () => { |
| 124 | + const { page } = setup() |
| 125 | + const { loggedError } = setupConsoleErrorSpy() |
| 126 | + |
| 127 | + page.result().remove() |
| 128 | + page.enterValueAndSubmit('2,2') |
| 129 | + |
| 130 | + expect(await vi.waitFor(loggedError)) |
| 131 | + .toStrictEqual(['missing result element']) |
| 132 | + }) |
62 | 133 | })
|
0 commit comments