Skip to content

Commit

Permalink
Merge pull request #63 from mbland/add-js-implementation
Browse files Browse the repository at this point in the history
Add temporary in-browser calculator implementation
  • Loading branch information
mbland authored Dec 19, 2023
2 parents 9f4faad + 3c7a61a commit 5a22ec9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
6 changes: 5 additions & 1 deletion strcalc/src/main/frontend/components/calculators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
23 changes: 18 additions & 5 deletions strcalc/src/main/frontend/components/calculators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"')
)
})
})

0 comments on commit 5a22ec9

Please sign in to comment.