62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
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);
|
|
});
|
|
});
|