astro-reactive-form/packages/form/test/Form.astro.test.js
Ayo Ayco 4dc020027f
feat: create astro reactive validator (#90)
* feat: initial validator component

* chore: fix eslint for validator

* chore: update package info for validator

* chore: remove vscode settings for docs

* chore: put docs and demo into apps

* chore: move package scope @astro-reactive

* test: update tests for validator

* feat: validator functions, hooks

* feat: validator sets haserrors attribute

* feat: use data-validator attributes

* feat: showValidationHints

* feature: add logic for all validators

* refactor: remove Validator component usage

* docs(validator): initial readme

* chore: comment out unsupported validator

* docs(validator): update installation

* chore: package docs and publish

* chore: update deps

* docs: update npm info on docs

* docs(validator): update docs for validator

* fix(form): handle undefined form

* test(validator): update tests

* chore: organize files; update deps

* chore: fix build scripts
2022-10-15 16:32:02 +02:00

95 lines
2.4 KiB
JavaScript

import { expect } from 'chai';
import { describe, beforeEach, it } from 'mocha';
import { getComponentOutput } from 'astro-component-tester';
describe('Form.astro test', () => {
let component;
beforeEach(() => {
component = undefined;
});
describe('INPUT: formGroups', () => {
it('Should handle undefined formGroups prop', async () => {
// arrange
const expectedCount = 0;
const element = /<fieldset>/g;
const props = { formGroups: undefined };
component = await getComponentOutput('./Form.astro', props);
// act
const actualResult = cleanString(component.html);
const matches = actualResult.match(element) || [];
// assert
expect(matches.length).to.equal(expectedCount);
});
it('Should handle empty formGroups prop', async () => {
// arrange
const expectedCount = 0;
const element = /<fieldset>/g;
const props = { formGroups: [] };
component = await getComponentOutput('./Form.astro', props);
// act
const actualResult = cleanString(component.html);
const matches = actualResult.match(element) || [];
// assert
expect(matches.length).to.equal(expectedCount);
});
it('Should render a fieldset for each form group', async () => {
// arrange
const expectedCount = 3;
const element = /<fieldset>/g;
const fakeFormGroup = {
controls: [
{
type: 'checkbox',
name: 'fake-checkbox',
label: 'FAKE CHECKBOX',
},
],
};
const props = { formGroups: Array(expectedCount).fill(fakeFormGroup) };
component = await getComponentOutput('./Form.astro', props);
// act
const actualResult = cleanString(component.html);
const matches = actualResult.match(element) || [];
// assert
expect(matches.length).to.equal(expectedCount);
});
it('Should not render a fieldset for a single form group', async () => {
// arrange
const expectedCount = 0;
const element = /<fieldset>/g;
const fakeFormGroup = {
controls: [
{
type: 'checkbox',
name: 'fake-checkbox',
label: 'FAKE CHECKBOX',
},
],
};
const props = { formGroups: fakeFormGroup };
component = await getComponentOutput('./Form.astro', props);
// act
const actualResult = cleanString(component.html);
const matches = actualResult.match(element) || [];
// assert
expect(matches.length).to.equal(expectedCount);
});
});
});
function cleanString(str) {
return str.replace(/\s/g, '');
}