-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for getSubmittedFormData()
- Loading branch information
1 parent
7d1750f
commit cf2cac8
Showing
4 changed files
with
289 additions
and
2 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
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,65 @@ | ||
import {describe, expect, it, vi} from 'vitest'; | ||
import {testOptions} from '$lib/TestOptions'; | ||
import {getSubmittedFormData} from "$lib"; | ||
|
||
describe('Submitted form data', () => { | ||
it( | ||
'does not work if no target is specified', | ||
() => { | ||
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => undefined); | ||
|
||
const submitted = getSubmittedFormData({} as unknown as SubmitEvent); | ||
|
||
expect(submitted).toStrictEqual({}); | ||
expect(consoleError).toHaveBeenCalledOnce(); | ||
expect(consoleError).toHaveBeenLastCalledWith('No form target specified. Did you forget to inject the proper SubmitEvent to the function?'); | ||
}, | ||
testOptions | ||
); | ||
|
||
it( | ||
'can handle empty input', | ||
() => { | ||
const submitted = getSubmittedFormData(mockSubmitEvent()); | ||
|
||
expect(submitted).toStrictEqual({}); | ||
}, | ||
testOptions | ||
); | ||
|
||
it( | ||
'can handle simple input', | ||
() => { | ||
const submitted = getSubmittedFormData(mockSubmitEvent([ | ||
['title', 'Some title'], | ||
['description', 'Some description'], | ||
])); | ||
|
||
expect(submitted).toStrictEqual({ | ||
'title': 'Some title', | ||
'description': 'Some description', | ||
}); | ||
}, | ||
testOptions | ||
); | ||
}); | ||
|
||
function mockSubmitEvent(submittedData: Array<[string, string]> = []) { | ||
const form = document.createElement('form'); | ||
|
||
const submitter = document.createElement('button'); | ||
submitter.type = 'submit'; | ||
form.appendChild(submitter); | ||
|
||
submittedData.forEach(([key, value]) => { | ||
const input = document.createElement('input'); | ||
input.name = key; | ||
input.value = value; | ||
form.appendChild(input); | ||
}); | ||
|
||
return { | ||
submitter: submitter, | ||
target: form, | ||
} as unknown as SubmitEvent; | ||
} |
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
Oops, something went wrong.