test: caminhos-base de lib, hooks e services
This commit is contained in:
1 parent
9d911603f7
commit
adc37e1921
18 files changed
+2961
-142
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { useOperatingSystem } from './use-operating-system';
|
||||
|
||||
vi.mock('@tauri-apps/api/core', () => ({ invoke: vi.fn() }));
|
||||
|
||||
const invokeMock = vi.mocked(invoke);
|
||||
|
||||
function setUserAgent(ua: string) {
|
||||
vi.spyOn(navigator, 'userAgent', 'get').mockReturnValue(ua);
|
||||
}
|
||||
|
||||
async function detect() {
|
||||
const { result } = renderHook(() => useOperatingSystem());
|
||||
await waitFor(() => expect(result.current.loading).toBe(false));
|
||||
return result.current.os;
|
||||
}
|
||||
|
||||
const UA = {
|
||||
windows: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
|
||||
mac: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0)',
|
||||
linux: 'Mozilla/5.0 (X11; Linux x86_64)',
|
||||
other: 'Mozilla/5.0 (X11; FreeBSD amd64)',
|
||||
};
|
||||
|
||||
describe('useOperatingSystem / detectOS', () => {
|
||||
beforeEach(() => {
|
||||
invokeMock.mockReset();
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
it.each(['linux', 'windows', 'mac'])('backend responde "%s": usa a resposta do backend', async (os) => {
|
||||
invokeMock.mockResolvedValue(os);
|
||||
setUserAgent(UA.other);
|
||||
expect(await detect()).toBe(os);
|
||||
expect(invokeMock).toHaveBeenCalledWith('get_operating_system');
|
||||
});
|
||||
|
||||
it.each([
|
||||
['windows', UA.windows],
|
||||
['mac', UA.mac],
|
||||
['linux', UA.linux],
|
||||
['windows', UA.other],
|
||||
])('backend responde valor desconhecido: user agent decide (%s)', async (expected, ua) => {
|
||||
invokeMock.mockResolvedValue('freebsd');
|
||||
setUserAgent(ua);
|
||||
expect(await detect()).toBe(expected);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['windows', UA.windows],
|
||||
['mac', UA.mac],
|
||||
['linux', UA.linux],
|
||||
['windows', UA.other],
|
||||
])('invoke falha (sem Tauri): user agent decide (%s)', async (expected, ua) => {
|
||||
invokeMock.mockRejectedValue(new Error('no tauri'));
|
||||
setUserAgent(ua);
|
||||
expect(await detect()).toBe(expected);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user