-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpage.test.ts
More file actions
158 lines (140 loc) · 4.89 KB
/
Copy pathpage.test.ts
File metadata and controls
158 lines (140 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/svelte';
import Page from './+page.svelte';
const mockFetch = vi.fn();
vi.stubGlobal('fetch', mockFetch);
function jsonResponse(data: unknown, status = 200) {
return new Response(JSON.stringify(data), {
status,
statusText: status === 200 ? 'OK' : 'Error',
headers: { 'Content-Type': 'application/json' }
});
}
const sampleSettings = {
company_name: 'Test OSVC',
title: 'Ing.',
first_name: 'Jan',
last_name: 'Novak',
ico: '12345678',
dic: 'CZ12345678',
vat_registered: 'true',
street: 'Hlavni',
house_number: '1',
city: 'Praha',
zip: '10000',
email: 'test@example.com',
phone: '+420123456789',
databox_id: '7rgmm99',
bank_account: '123456789',
bank_code: '0100',
iban: 'CZ1234567890',
swift: 'KOMBCZPP',
c_ufo: '464',
c_pracufo: '3305',
c_okec: '582900',
cssz_code: '775',
cssz_variable_symbol: '75660248'
};
beforeEach(() => {
vi.useFakeTimers();
mockFetch.mockReset();
mockFetch.mockResolvedValue(jsonResponse(sampleSettings));
});
afterEach(() => {
cleanup();
vi.useRealTimers();
});
describe('Settings Firma Page', () => {
it('loads settings on mount', async () => {
render(Page);
await waitFor(() => {
expect(mockFetch).toHaveBeenCalledWith(
expect.stringContaining('/api/v1/settings'),
expect.any(Object)
);
});
});
it('renders form with business info fields after loading', async () => {
render(Page);
await waitFor(() => {
expect(document.querySelector('#company_name')).toBeInTheDocument();
});
expect(document.querySelector('#first_name')).toBeInTheDocument();
expect(document.querySelector('#last_name')).toBeInTheDocument();
expect(document.querySelector('#title')).toBeInTheDocument();
expect(document.querySelector('#ico')).toBeInTheDocument();
expect(document.querySelector('#dic')).toBeInTheDocument();
expect(document.querySelector('#vat_registered')).toBeInTheDocument();
expect(document.querySelector('#street')).toBeInTheDocument();
expect(document.querySelector('#house_number')).toBeInTheDocument();
expect(document.querySelector('#city')).toBeInTheDocument();
expect(document.querySelector('#zip')).toBeInTheDocument();
expect(document.querySelector('#email')).toBeInTheDocument();
expect(document.querySelector('#phone')).toBeInTheDocument();
expect(document.querySelector('#databox_id')).toBeInTheDocument();
expect(document.querySelector('#bank_account')).toBeInTheDocument();
expect(document.querySelector('#bank_code')).toBeInTheDocument();
expect(document.querySelector('#iban')).toBeInTheDocument();
expect(document.querySelector('#swift')).toBeInTheDocument();
expect(document.querySelector('#c_ufo')).toBeInTheDocument();
expect(document.querySelector('#c_pracufo')).toBeInTheDocument();
expect(document.querySelector('#c_okec')).toBeInTheDocument();
expect(document.querySelector('#cssz_code')).toBeInTheDocument();
expect(document.querySelector('#cssz_variable_symbol')).toBeInTheDocument();
});
it('shows settings values in inputs', async () => {
render(Page);
await waitFor(() => {
const nameInput = document.querySelector('#company_name') as HTMLInputElement;
expect(nameInput).toBeInTheDocument();
expect(nameInput.value).toBe('Test OSVC');
});
const icoInput = document.querySelector('#ico') as HTMLInputElement;
expect(icoInput.value).toBe('12345678');
const vatCheckbox = document.querySelector('#vat_registered') as HTMLInputElement;
expect(vatCheckbox.checked).toBe(true);
});
it('save calls settingsApi.update (PUT /api/v1/settings)', async () => {
render(Page);
await waitFor(() => {
expect(document.querySelector('#company_name')).toBeInTheDocument();
});
mockFetch.mockResolvedValue(jsonResponse(sampleSettings));
const form = document.querySelector('form')!;
await fireEvent.submit(form);
await waitFor(() => {
const putCall = mockFetch.mock.calls.find(
(call: any[]) =>
typeof call[0] === 'string' &&
call[0].includes('/api/v1/settings') &&
call[1]?.method === 'PUT'
);
expect(putCall).toBeDefined();
});
});
it('save completes without error', async () => {
render(Page);
await waitFor(() => {
expect(document.querySelector('#company_name')).toBeInTheDocument();
});
mockFetch.mockResolvedValue(jsonResponse(sampleSettings));
const form = document.querySelector('form')!;
await fireEvent.submit(form);
await waitFor(() => {
const putCall = mockFetch.mock.calls.find(
(call: any[]) =>
typeof call[0] === 'string' &&
call[0].includes('/api/v1/settings') &&
call[1]?.method === 'PUT'
);
expect(putCall).toBeDefined();
});
});
it('error state on load failure', async () => {
mockFetch.mockRejectedValue(new Error('Network error'));
render(Page);
await waitFor(() => {
expect(screen.getByText('Network error')).toBeInTheDocument();
});
});
});