Skip to content

Commit cb15ebc

Browse files
committed
test(desktop): show the prevent-sleep switch only on a device that runs chats in the background
1 parent 4f3b89f commit cb15ebc

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/settings/components/desktop
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*/
4+
import { act, type ReactNode } from 'react'
5+
import { emcnMock } from '@sim/testing/mocks/emcn.mock'
6+
import { libDesktopMock, libDesktopMockFns } from '@sim/testing/mocks/lib-desktop.mock'
7+
import { nextNavigationMock } from '@sim/testing/mocks/next-navigation.mock'
8+
import { createRoot, type Root } from 'react-dom/client'
9+
import { afterEach, describe, expect, it, vi } from 'vitest'
10+
11+
vi.mock('@sim/emcn', () => ({
12+
...emcnMock,
13+
Label: ({ children, htmlFor }: { children?: ReactNode; htmlFor?: string }) => (
14+
<label htmlFor={htmlFor}>{children}</label>
15+
),
16+
Switch: ({ id, checked }: { id: string; checked: boolean }) => (
17+
<button type='button' role='switch' id={id} aria-checked={checked} />
18+
),
19+
}))
20+
vi.mock('next/navigation', () => nextNavigationMock)
21+
vi.mock('@/lib/desktop', () => libDesktopMock)
22+
vi.mock('@/app/workspace/[workspaceId]/settings/components/settings-panel', () => ({
23+
SettingsPanel: ({ children }: { children?: ReactNode }) => <main>{children}</main>,
24+
}))
25+
vi.mock(
26+
'@/app/workspace/[workspaceId]/settings/components/settings-section/settings-section',
27+
() => ({
28+
SettingsSection: ({ children }: { children?: ReactNode }) => <section>{children}</section>,
29+
})
30+
)
31+
32+
import { Desktop } from '@/app/workspace/[workspaceId]/settings/components/desktop/desktop'
33+
34+
const PREFERENCES = {
35+
launchAtLogin: false,
36+
trayEnabled: true,
37+
autoDownloadUpdates: true,
38+
notificationsEnabled: true,
39+
notificationSounds: true,
40+
notificationsOnlyWhenUnfocused: true,
41+
preventSleepWhileRunning: true,
42+
}
43+
44+
let root: Root | null = null
45+
46+
/** Renders the settings page in a shell whose executor has, or lacks, a registered device. */
47+
async function render(device: { deviceId: string } | null) {
48+
libDesktopMockFns.mockGetDesktopBridge.mockReturnValue({
49+
settings: {
50+
getPreferences: async () => PREFERENCES,
51+
setPreventSleepWhileRunning: async () => PREFERENCES,
52+
},
53+
desktopExecutor: { getDevice: async () => device },
54+
})
55+
;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
56+
const container = document.createElement('div')
57+
document.body.appendChild(container)
58+
root = createRoot(container)
59+
await act(async () => root?.render(<Desktop />))
60+
return container
61+
}
62+
63+
afterEach(() => {
64+
act(() => root?.unmount())
65+
root = null
66+
libDesktopMockFns.mockGetDesktopBridge.mockReturnValue(undefined)
67+
})
68+
69+
describe('Desktop settings: prevent sleep', () => {
70+
it('hides the switch when Sim runs no chats on this device in the background', async () => {
71+
const container = await render(null)
72+
73+
expect(container.querySelector('#desktop-launch-at-login')).not.toBeNull()
74+
expect(container.querySelector('#desktop-prevent-sleep')).toBeNull()
75+
})
76+
77+
it('shows the switch once this device runs chats in the background', async () => {
78+
const container = await render({ deviceId: 'device-1' })
79+
80+
expect(container.querySelector('#desktop-prevent-sleep')?.getAttribute('aria-checked')).toBe(
81+
'true'
82+
)
83+
})
84+
})

0 commit comments

Comments
 (0)