dphcko-frontend/src/lib/calculator/Calculator.test.ts
2023-09-08 10:46:34 +02:00

81 lines
2.0 KiB
TypeScript

import Calculator from '$lib/calculator/Calculator.svelte';
import { describe, it } from 'vitest';
import '@testing-library/jest-dom';
import { fireEvent, render, screen } from '@testing-library/svelte';
function initFields() {
render(Calculator);
return {
vatRate: screen.getByLabelText('Sazba DPH (%)', { selector: 'input' }),
basePrice: screen.getByLabelText('Cena bez DPH (Kč)', { selector: 'input' }),
finalPrice: screen.getByLabelText('Cena s DPH (Kč)', { selector: 'input' })
};
}
describe('Test Calculator.svelte', async () => {
it('Initial prices should be zero', async () => {
const { vatRate, basePrice, finalPrice } = initFields();
expect(vatRate).toHaveValue(0);
expect(basePrice).toHaveValue(0);
expect(finalPrice).toHaveValue(0);
});
it('Calculation #1 should be correct (225*1.21=272.25)', async () => {
const { vatRate, basePrice, finalPrice } = initFields();
expect(vatRate).toHaveValue(0);
expect(basePrice).toHaveValue(0);
expect(finalPrice).toHaveValue(0);
await fireEvent.input(vatRate, {
target: {
value: 21
}
});
expect(vatRate).toHaveValue(21);
expect(basePrice).toHaveValue(0);
expect(finalPrice).toHaveValue(0);
await fireEvent.input(basePrice, {
target: {
value: 225
}
});
expect(vatRate).toHaveValue(21);
expect(basePrice).toHaveValue(225);
expect(finalPrice).toHaveValue(272.25);
});
it('Calculation #2 should be correct (272.25/1.21=225)', async () => {
const { vatRate, basePrice, finalPrice } = initFields();
expect(vatRate).toHaveValue(0);
expect(basePrice).toHaveValue(0);
expect(finalPrice).toHaveValue(0);
await fireEvent.input(vatRate, {
target: {
value: 21
}
});
expect(vatRate).toHaveValue(21);
expect(basePrice).toHaveValue(0);
expect(finalPrice).toHaveValue(0);
await fireEvent.input(finalPrice, {
target: {
value: 272.25
}
});
expect(vatRate).toHaveValue(21);
expect(basePrice).toHaveValue(225);
expect(finalPrice).toHaveValue(272.25);
});
});