From 3c7a61a811ac6769bcdc56cfbbd46b0a5198fa59 Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Mon, 18 Dec 2023 21:19:23 -0500 Subject: [PATCH] Add temporary in-browser calculator implementation Like the Java version on the backend, this implementation merely throws an error including its input string. --- .../main/frontend/components/calculators.js | 6 ++++- .../frontend/components/calculators.test.js | 23 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/strcalc/src/main/frontend/components/calculators.js b/strcalc/src/main/frontend/components/calculators.js index 6cd6d93..4d36f01 100644 --- a/strcalc/src/main/frontend/components/calculators.js +++ b/strcalc/src/main/frontend/components/calculators.js @@ -10,10 +10,14 @@ export const DEFAULT_ENDPOINT = './add' const defaultPost = async (data)=> postFormData(DEFAULT_ENDPOINT, data) +const tempCalculator = async (data) => Promise.reject(new Error( + `Temporary in-browser calculator received: "${data.get('numbers')}"` +)) + /** * Collection of production String Calculator implementations */ export default { 'api': { label: 'Tomcat backend API (Java)', impl: defaultPost }, - 'browser': { label: 'In-browser (JavaScript)', impl: defaultPost } + 'browser': { label: 'In-browser (JavaScript)', impl: tempCalculator } } diff --git a/strcalc/src/main/frontend/components/calculators.test.js b/strcalc/src/main/frontend/components/calculators.test.js index 2082374..435c234 100644 --- a/strcalc/src/main/frontend/components/calculators.test.js +++ b/strcalc/src/main/frontend/components/calculators.test.js @@ -11,15 +11,28 @@ import setupFetchStub from '../test/fetch-stub' import { postOptions } from './request' describe('calculators', () => { + const setupData = (numbersStr) => { + const data = new FormData() + data.append('numbers', numbersStr) + return data + } + afterEach(() => { vi.unstubAllGlobals() }) test('defaultPost requests expected backend', async () => { - const data = new FormData() - const fetchStub = setupFetchStub(JSON.stringify({ ok: true })) - data.append('want', 'status') + const data = setupData('2,2') + const fetchStub = setupFetchStub(JSON.stringify({ result: 5 })) - await expect(calculators.api.impl(data)).resolves.toEqual({ ok: true }) + await expect(calculators.api.impl(data)).resolves.toEqual({ result: 5 }) expect(fetchStub).toHaveBeenCalledWith( - DEFAULT_ENDPOINT, postOptions({ want: 'status' })) + DEFAULT_ENDPOINT, postOptions({ numbers: '2,2' })) + }) + + test('tempCalculator rejects with Error', async () => { + const data = setupData('2,2') + + await expect(calculators.browser.impl(data)).rejects.toThrow( + new Error('Temporary in-browser calculator received: "2,2"') + ) }) })