-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from mbland/calculator-component
Add Calculator frontend component
- Loading branch information
Showing
6 changed files
with
134 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{{!-- | ||
This Source Code Form is subject to the terms of the Mozilla Public | ||
License, v. 2.0. If a copy of the MPL was not distributed with this | ||
file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
--}} | ||
<form action="{{ apiUrl }}" method="post"> | ||
<label for="numbers" name="numbersLabel" id="numbersLabel"> | ||
Enter the string of numbers to add: | ||
</label> | ||
<input type="text" name="numbers" id="numbers" required /> | ||
<input type="submit" name="numbersSubmit" id="numbersSubmit" value="Add" /> | ||
</form> | ||
<div class="result"><p>The result will appear here.</p></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import Template from './calculator.hbs' | ||
|
||
export default class Calculator { | ||
/** | ||
* Initializes the Calculator within the document. | ||
* @param {object} params - parameters made available to all initializers | ||
* @param {Element} params.appElem - parent Element containing all components | ||
* @param {string} params.apiUrl - API backend server URL | ||
* @param {Function} params.postForm - posts form data to API | ||
*/ | ||
static init({ appElem, apiUrl, postForm }) { | ||
const t = Template({ apiUrl }) | ||
const [ form, resultElem ] = t.children | ||
|
||
appElem.appendChild(t) | ||
form.addEventListener( | ||
'submit', e => this.#submitRequest(e, resultElem, postForm) | ||
) | ||
} | ||
|
||
// https://simonplend.com/how-to-use-fetch-to-post-form-data-as-json-to-your-api/ | ||
static async #submitRequest(event, resultElem, postForm) { | ||
event.preventDefault() | ||
|
||
const result = resultElem.querySelector('p') | ||
|
||
try { | ||
const response = await postForm(event.currentTarget) | ||
result.textContent = `Result: ${response.result}` | ||
} catch (err) { | ||
result.textContent = err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* eslint-env browser, node, jest, vitest */ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import Calculator from './calculator' | ||
import { afterAll, afterEach, describe, expect, test, vi } from 'vitest' | ||
import { resolvedUrl } from '../test/helpers.js' | ||
import StringCalculatorPage from '../test/page' | ||
|
||
// @vitest-environment jsdom | ||
describe('Calculator', () => { | ||
const page = StringCalculatorPage.new() | ||
|
||
const setup = () => { | ||
const postForm = vi.fn() | ||
Calculator.init({ appElem: page.appElem, apiUrl: './add', postForm }) | ||
return { page, postForm } | ||
} | ||
|
||
afterEach(() => page.clear()) | ||
afterAll(() => page.remove()) | ||
|
||
test('renders form and result placeholder', async () => { | ||
const { page } = setup() | ||
|
||
expect(page.form()).not.toBeNull() | ||
expect(page.form().action).toBe(resolvedUrl('./add')) | ||
}) | ||
|
||
test('updates result placeholder with successful result', async () => { | ||
const { page, postForm } = setup() | ||
postForm.mockResolvedValueOnce({ result: 5 }) | ||
|
||
const result = vi.waitFor(page.enterValueAndSubmit('2,2')) | ||
|
||
await expect(result).resolves.toBe('Result: 5') | ||
expect(postForm).toHaveBeenCalledWith(page.form()) | ||
}) | ||
|
||
test('updates result placeholder with error message', async () => { | ||
const { page, postForm } = setup() | ||
postForm.mockRejectedValueOnce(new Error('D\'oh!')) | ||
|
||
const result = vi.waitFor(page.enterValueAndSubmit('2,2')) | ||
|
||
await expect(result).resolves.toBe('Error: D\'oh!') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters