Skip to content

Commit

Permalink
👽 [#724] Vitest doesn't support the done callback
Browse files Browse the repository at this point in the history
Instead, the most elegant way to handle this is to use async/await
syntax.
  • Loading branch information
sergei-maertens committed Nov 17, 2024
1 parent 02cdabd commit 8c753a8
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 382 deletions.
41 changes: 15 additions & 26 deletions src/jstests/formio/components/currency.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ from 'lodash';
import React from 'react';
import {Formio} from 'react-formio';

import OpenFormsModule from 'formio/module';
Expand All @@ -10,44 +9,34 @@ import {currencyForm} from './fixtures/currency';
Formio.use(OpenFormsModule);

describe('Currency Component', () => {
test('Currency component with 0 decimalLimit formatted correctly', done => {
test('Currency component with 0 decimalLimit formatted correctly', async () => {
let formJSON = _.cloneDeep(currencyForm);

const element = document.createElement('div');

Formio.createForm(element, formJSON)
.then(form => {
form.setPristine(false);
const component = form.getComponent('currency');
const formattedValue = component.getValueAsString(1);
const form = await Formio.createForm(element, formJSON);
form.setPristine(false);
const component = form.getComponent('currency');
const formattedValue = component.getValueAsString(1);

expect(formattedValue).toEqual('€1');

done();
})
.catch(done);
expect(formattedValue).toEqual('€1');
});

test('#2903 - Emptying currency component results in null value in data', done => {
test('#2903 - Emptying currency component results in null value in data', async () => {
let formJSON = _.cloneDeep(currencyForm);

const element = document.createElement('div');

Formio.createForm(element, formJSON)
.then(form => {
form.setPristine(false);
const component = form.getComponent('currency');
component.setValue(13);

expect(form._data['currency']).toEqual(13);
const form = await Formio.createForm(element, formJSON);
form.setPristine(false);
const component = form.getComponent('currency');
component.setValue(13);

component.dataValue = null;
expect(form._data['currency']).toEqual(13);

// null, instead of undefined (default Formio behaviour which removes the key from the data)
expect(form._data['currency']).toEqual(null);
component.dataValue = null;

done();
})
.catch(done);
// null, instead of undefined (default Formio behaviour which removes the key from the data)
expect(form._data['currency']).toEqual(null);
});
});
20 changes: 5 additions & 15 deletions src/jstests/formio/components/date.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {MinMaxDateValidator} from 'formio/validators/minMaxDateAndDatetimeValida
const FormioComponent = Formio.Components.components.component;

describe('Date Component', () => {
test('Date validator: check min date', done => {
test('Date validator: check min date', () => {
const component = {
label: 'date',
key: 'date',
Expand All @@ -33,11 +33,9 @@ describe('Date Component', () => {
const isValid2 = MinMaxDateValidator.check(componentInstance, {}, '2024-01-01');

expect(isValid2).toBeTruthy();

done();
});

test('Date validator: check max date', done => {
test('Date validator: check max date', () => {
const component = {
label: 'date',
key: 'date',
Expand All @@ -64,11 +62,9 @@ describe('Date Component', () => {
const isValid2 = MinMaxDateValidator.check(componentInstance, {}, '2020-01-01');

expect(isValid2).toBeTruthy();

done();
});

test('Date validator: check max date including the current one', done => {
test('Date validator: check max date including the current one', () => {
const component = {
label: 'date',
key: 'date',
Expand All @@ -88,11 +84,9 @@ describe('Date Component', () => {
const isValid1 = MinMaxDateValidator.check(componentInstance, {}, '2023-09-08');

expect(isValid1).toBeTruthy();

done();
});

test('Date validator: error message', done => {
test('Date validator: error message', () => {
const component = {
label: 'date',
key: 'date',
Expand Down Expand Up @@ -137,11 +131,9 @@ describe('Date Component', () => {
MinMaxDateValidator.message(componentInstance);

expect(mockTranslation.mock.calls[1][0]).toEqual('maxDate');

done();
});

test('Date validator: check max date AND min date', done => {
test('Date validator: check max date AND min date', () => {
const component = {
label: 'date',
key: 'date',
Expand Down Expand Up @@ -171,7 +163,5 @@ describe('Date Component', () => {
expect(
componentInstance.openForms.validationErrorContext.minMaxDateAndDatetimeValidatorErrorKey
).toContain('minDate');

done();
});
});
83 changes: 35 additions & 48 deletions src/jstests/formio/components/file.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,71 +25,58 @@ const maxNFilesForm = {
};

describe('Multiple File Component', () => {
test('Uploading 1 file gives no error', done => {
test('Uploading 1 file gives no error', async () => {
let formJSON = _.cloneDeep(maxNFilesForm);

const element = document.createElement('div');

Formio.createForm(element, formJSON)
.then(form => {
form.setPristine(false);
const component = form.getComponent('multipleFiles');
component.upload([{name: 'File 1', size: '50'}]);
const form = await Formio.createForm(element, formJSON);
form.setPristine(false);
const component = form.getComponent('multipleFiles');
component.upload([{name: 'File 1', size: '50'}]);

expect(!!component.error).toBeFalsy();

done();
})
.catch(done);
expect(!!component.error).toBeFalsy();
});

test('Uploading 3 files gives an error', done => {
test('Uploading 3 files gives an error', async () => {
let formJSON = _.cloneDeep(maxNFilesForm);

const element = document.createElement('div');

Formio.createForm(element, formJSON)
.then(form => {
form.setPristine(false);
const component = form.getComponent('multipleFiles');
component.upload([
{name: 'File 1', size: '50'},
{name: 'File 2', size: '50'},
{name: 'File 3', size: '50'},
]);

expect(!!component.error).toBeTruthy();
expect(component.error.message).toEqual(
'Too many files added. The maximum allowed number of files is 2.'
);
done();
})
.catch(done);
const form = await Formio.createForm(element, formJSON);
form.setPristine(false);
const component = form.getComponent('multipleFiles');
component.upload([
{name: 'File 1', size: '50'},
{name: 'File 2', size: '50'},
{name: 'File 3', size: '50'},
]);

expect(!!component.error).toBeTruthy();
expect(component.error.message).toEqual(
'Too many files added. The maximum allowed number of files is 2.'
);
});

// GH-4222
test('Uploading 1 file then 2 files gives an error', done => {
test('Uploading 1 file then 2 files gives an error', async () => {
let formJSON = _.cloneDeep(maxNFilesForm);

const element = document.createElement('div');

Formio.createForm(element, formJSON)
.then(form => {
form.setPristine(false);
const component = form.getComponent('multipleFiles');
component.dataValue.push({name: 'File 1', size: '50'});

component.upload([
{name: 'File 2', size: '50'},
{name: 'File 3', size: '50'},
]);

expect(!!component.error).toBeTruthy();
expect(component.error.message).toEqual(
'Too many files added. The maximum allowed number of files is 2.'
);
done();
})
.catch(done);
const form = await Formio.createForm(element, formJSON);
form.setPristine(false);
const component = form.getComponent('multipleFiles');
component.dataValue.push({name: 'File 1', size: '50'});

component.upload([
{name: 'File 2', size: '50'},
{name: 'File 3', size: '50'},
]);

expect(!!component.error).toBeTruthy();
expect(component.error.message).toEqual(
'Too many files added. The maximum allowed number of files is 2.'
);
});
});
100 changes: 37 additions & 63 deletions src/jstests/formio/components/ibanfield.spec.js
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();
});
});
Loading

0 comments on commit 8c753a8

Please sign in to comment.