import { Declaform } from '../src';
const MOVIE_FORM_NO_SRC = `
`;
const MOVIE_FORM_HTTP = `
`;
describe('Declaform', () => {
afterEach(() => {
document.body.innerHTML = '';
})
it('Should mount.', () => {
document.body.innerHTML = MOVIE_FORM_NO_SRC;
expect(document.getElementById('form')).toBeTruthy();
})
it('.toObject() should serialize form into a JS object', () => {
document.body.innerHTML = MOVIE_FORM_NO_SRC;
const form = document.getElementById('form') as Declaform;
setInputValue('name', 'Monty Python');
setInputValue('genre', 'comedy');
setRadioValue('rating.type', 'user');
setInputValue('rating-score', '5');
const obj = form.toObject();
expect(obj).toMatchObject({
name: 'Monty Python',
genre: 'comedy',
rating: {
score: 5,
type: 'user',
},
});
});
it('.hydrate() should populate input fields from a JS object', () => {
document.body.innerHTML = MOVIE_FORM_NO_SRC;
const form = document.getElementById('form') as Declaform;
form.hydrate({
name: 'Jaws',
genre: 'horror',
rating: {
score: 5,
type: 'critic',
}
});
expect(getInputValue('name')).toBe('Jaws')
expect(getInputValue('genre')).toBe('horror')
expect(getInputValue('rating-score')).toBe('5');
expect(document.getElementById('rating-type-critic')?.hasAttribute('checked')).toBe(true)
});
it('Supplying "src" attribute should hydrate form with the supplied endpoint.', async () => {
globalThis.fetch = jest.fn().mockResolvedValue({
status: 200,
ok: true,
json: () => Promise.resolve({
name: 'Jaws',
genre: 'horror',
rating: {
score: 5,
type: 'critic',
}
})
})
document.body.innerHTML = MOVIE_FORM_HTTP;
await new Promise((resolve) => setTimeout(resolve, 0))
expect(getInputValue('name')).toBe('Jaws')
expect(getInputValue('genre')).toBe('horror')
expect(getInputValue('rating-score')).toBe('5');
expect(document.getElementById('rating-type-critic')?.hasAttribute('checked')).toBe(true)
});
});
function setInputValue(id: string, value: string) {
const input = document.getElementById(id);
if (!input) {
throw new Error(`No input with id "${id}"!`);
}
if (!('value' in input)) {
throw new Error(`Tag with id "${id}" does not have "value" attribute!`);
}
input.value = value;
}
function getInputValue(id: string) {
const input = document.getElementById(id);
if (!input) {
throw new Error(`No input with id "${id}"!`);
}
if (!('value' in input)) {
throw new Error(`Tag with id "${id}" does not have "value" attribute!`);
}
return input.value;
}
function setRadioValue(name: string, value: string) {
const radios = document.querySelectorAll(`[name="${name}"]`);
if (!radios) {
throw new Error(`No radios with name "${name}"!`);
}
radios.forEach(radio => {
if (!('value' in radio)) {
throw new Error('Radio option does not have "value" attribute!');
}
radio.removeAttribute('checked');
})
const option = ([...radios] as HTMLInputElement[]).find(x => x.value);
if (!option) {
throw new Error(`No option with value "${value}"`);
}
option.setAttribute('checked', '');
}