-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👽 [#724] Vitest doesn't support the done callback
Instead, the most elegant way to handle this is to use async/await syntax.
- Loading branch information
1 parent
fd24d00
commit f20718c
Showing
9 changed files
with
239 additions
and
382 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
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 |
---|---|---|
@@ -1,87 +1,61 @@ | ||
import _ from 'lodash'; | ||
import React from 'react'; | ||
import {Formio} from 'react-formio'; | ||
|
||
import OpenFormsModule from 'formio/module'; | ||
import {sleep} from 'utils'; | ||
|
||
import {iban, twoComponentForm} from './fixtures/iban'; | ||
|
||
// Use our custom components | ||
Formio.use(OpenFormsModule); | ||
|
||
describe('IBAN Component', () => { | ||
test('IBAN component validation', done => { | ||
test.each([ | ||
// valid values | ||
['BR15 0000 0000 0000 1093 2840 814 P2', true], | ||
['FR76 3000 6000 0112 3456 7890 189', true], | ||
['MU43 BOMM 0101 1234 5678 9101 000 MUR', true], | ||
['BR1500000000000010932840814P2', true], | ||
['FR76-3000-6000-0112-3456-7890-189', true], | ||
// invalid values | ||
['BR15 0000 0000 0000 1093 2840 814 00', false], | ||
['FR76 3000 6000 0112 3456 7890 000', false], | ||
['MU43 BOMM 0101 1234 5678 9101 000 MUR 00', false], | ||
['BR150000000000001093284081400', false], | ||
['FR76-3000-6000-0112-3456-7890-000', false], | ||
])('IBAN component validation (%s, valid: %s)', async (value, valid) => { | ||
const formJSON = _.cloneDeep(iban); | ||
|
||
const validValues = [ | ||
'BR15 0000 0000 0000 1093 2840 814 P2', | ||
'FR76 3000 6000 0112 3456 7890 189', | ||
'MU43 BOMM 0101 1234 5678 9101 000 MUR', | ||
'BR1500000000000010932840814P2', | ||
'FR76-3000-6000-0112-3456-7890-189', | ||
]; | ||
const element = document.createElement('div'); | ||
|
||
const invalidValues = [ | ||
'BR15 0000 0000 0000 1093 2840 814 00', | ||
'FR76 3000 6000 0112 3456 7890 000', | ||
'MU43 BOMM 0101 1234 5678 9101 000 MUR 00', | ||
'BR150000000000001093284081400', | ||
'FR76-3000-6000-0112-3456-7890-000', | ||
]; | ||
const form = await Formio.createForm(element, formJSON); | ||
form.setPristine(false); | ||
const component = form.getComponent('iban'); | ||
const changed = component.setValue(value); | ||
expect(changed).toBeTruthy(); | ||
|
||
const testValidity = (values, valid) => { | ||
values.forEach(value => { | ||
const element = document.createElement('div'); | ||
await sleep(300); | ||
|
||
Formio.createForm(element, formJSON) | ||
.then(form => { | ||
form.setPristine(false); | ||
const component = form.getComponent('iban'); | ||
const changed = component.setValue(value); | ||
expect(changed).toBeTruthy(); | ||
|
||
setTimeout(() => { | ||
if (valid) { | ||
expect(!!component.error).toBeFalsy(); | ||
} else { | ||
expect(!!component.error).toBeTruthy(); | ||
expect(component.error.message).toEqual('Invalid IBAN'); | ||
} | ||
|
||
if (value === invalidValues[4]) { | ||
done(); | ||
} | ||
}, 300); | ||
}) | ||
.catch(done); | ||
}); | ||
}; | ||
|
||
testValidity(validValues, true); | ||
testValidity(invalidValues, false); | ||
if (valid) { | ||
expect(!!component.error).toBeFalsy(); | ||
} else { | ||
expect(!!component.error).toBeTruthy(); | ||
expect(component.error.message).toEqual('Invalid IBAN'); | ||
} | ||
}); | ||
|
||
test('IBAN validation not triggered by other components', done => { | ||
test('IBAN validation not triggered by other components', async () => { | ||
const formJSON = _.cloneDeep(twoComponentForm); | ||
|
||
const testValidity = () => { | ||
const element = document.createElement('div'); | ||
|
||
Formio.createForm(element, formJSON) | ||
.then(form => { | ||
form.setPristine(false); | ||
const component = form.getComponent('name'); | ||
const changed = component.setValue('John'); | ||
expect(changed).toBeTruthy(); | ||
const element = document.createElement('div'); | ||
|
||
setTimeout(() => { | ||
expect(!!component.error).toBeFalsy(); | ||
done(); | ||
}, 300); | ||
}) | ||
.catch(done); | ||
}; | ||
const form = await Formio.createForm(element, formJSON); | ||
form.setPristine(false); | ||
const component = form.getComponent('name'); | ||
const changed = component.setValue('John'); | ||
expect(changed).toBeTruthy(); | ||
|
||
testValidity(); | ||
await sleep(300); | ||
expect(!!component.error).toBeFalsy(); | ||
}); | ||
}); |
Oops, something went wrong.