Skip to content

Commit 0e4f7bb

Browse files
Various type fixes
1 parent 7e87bd9 commit 0e4f7bb

6 files changed

Lines changed: 39 additions & 18 deletions

File tree

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ export default {
3131
}
3232

3333
/**
34-
* @import { Config, ProjectConfig } from 'jest'
34+
* @import { Config } from 'jest'
3535
*/

manage_breast_screening/assets/js/check-in.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import setSubmit from './set-submit.js'
22

3-
/*
4-
Enhance an HTML form to intercept submit events and instead fetch the form action URL.
5-
If successful, show child elements with data-show-on-submit, and hide those with data-hide-on-submit.
6-
If unsuccessful, or the fetch times out, force a page refresh.
7-
*/
3+
/**
4+
* Enhance an HTML form to intercept submit events and instead fetch the form action URL.
5+
* If successful, show child elements with data-show-on-submit, and hide those with data-hide-on-submit.
6+
* If unsuccessful, or the fetch times out, force a page refresh.
7+
*/
88
class CheckIn {
9+
/**
10+
* @param {Element | null} [$root] - HTML element to use for component
11+
*/
912
constructor($root) {
10-
if (!$root) {
11-
throw Error('CheckIn initialised without a root element')
13+
if (!$root || !($root instanceof HTMLElement)) {
14+
throw new Error('CheckIn initialised without a root element')
15+
}
16+
17+
const $form = $root.querySelector('form')
18+
if (!$form) {
19+
throw new Error('CheckIn initialised without a form element')
1220
}
1321

1422
this.$root = $root
15-
this.$form = $root.querySelector('form')
23+
this.$form = $form
1624
this.$showOnSubmit = $root.querySelectorAll('[data-show-on-submit]')
1725
this.$hideOnSubmit = $root.querySelectorAll('[data-hide-on-submit]')
1826

manage_breast_screening/assets/js/set-submit.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,30 @@ const TIMEOUT = 2000
99
* @param {object} [options] - Handler options
1010
* @param {(this: HTMLFormElement) => void} [options.onBeforeSubmit] - Callback before submission
1111
* @param {(this: HTMLFormElement, response: Response) => void} [options.onSuccess] - Callback on successful response
12-
* @param {(this: HTMLFormElement, error: any) => void} [options.onError] - Callback on error
12+
* @param {(this: HTMLFormElement, error: Error) => void} [options.onError] - Callback on error
1313
*/
1414
export default ($form, options = {}) => {
1515
if (!$form || !($form instanceof HTMLFormElement)) {
16-
throw Error('setSubmit must be called with an HTMLFormElement')
16+
throw new Error('setSubmit must be called with an HTMLFormElement')
1717
}
1818

1919
const method = $form.method
2020
const action = $form.action
2121

2222
if (!method || !action) {
23-
throw Error('Form method and action must be defined')
23+
throw new Error('Form method and action must be defined')
2424
}
2525

26-
const doSubmit = async () => {
26+
async function doSubmit() {
2727
if (options.onBeforeSubmit) {
2828
options.onBeforeSubmit.call($form)
2929
}
3030

31+
/** @type {RequestInit} */
3132
const fetchOptions = { method: method, body: new FormData($form) }
32-
if (AbortSignal.timeout !== undefined) {
33+
34+
// Check for timeout support
35+
if ('AbortSignal' in window && 'timeout' in AbortSignal) {
3336
fetchOptions.signal = AbortSignal.timeout(TIMEOUT)
3437
}
3538

@@ -41,7 +44,7 @@ export default ($form, options = {}) => {
4144
throw new Error(`Response status: ${response.status}`)
4245
}
4346
} catch (e) {
44-
if (options.onError) {
47+
if (options.onError && e instanceof Error) {
4548
options.onError.apply($form, [e])
4649
}
4750
return

manage_breast_screening/assets/js/set-submit.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('setSubmit', () => {
1818
/** @type {Response} */
1919
let successResponse
2020

21+
/** @type {Error} */
2122
let error
2223

2324
beforeEach(() => {
@@ -58,7 +59,7 @@ describe('setSubmit', () => {
5859
await user.click(button)
5960

6061
expect(beforeSubmit).toBe(true)
61-
expect(successResponse.status).toEqual(200)
62+
expect(successResponse).toHaveProperty('status', 200)
6263
expect(error).toBeUndefined()
6364
})
6465

manage_breast_screening/templates/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default {
1212
},
1313
parserOptions: {
1414
// Note: Allow ES2015 for import/export syntax
15-
ecmaVersion: '2015'
15+
ecmaVersion: 2015
1616
},
1717
plugins: ['es-x'],
1818
rules: {

rollup.config.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import commonjs from '@rollup/plugin-commonjs'
22
import { nodeResolve } from '@rollup/plugin-node-resolve'
33
import terser from '@rollup/plugin-terser'
4-
import { babel } from '@rollup/plugin-babel';
4+
import { babel } from '@rollup/plugin-babel'
55

6+
/**
7+
* Rollup config
8+
*
9+
* @type {RollupOptions}
10+
*/
611
export default {
712
input: 'manage_breast_screening/assets/js/index.js',
813

@@ -40,3 +45,7 @@ export default {
4045
babel({ babelHelpers: 'bundled' }) // must come after commonjs
4146
]
4247
}
48+
49+
/**
50+
* @import { RollupOptions } from 'rollup'
51+
*/

0 commit comments

Comments
 (0)