Skip to content

Commit c1cd150

Browse files
committed
add useRoomExp tests
1 parent 3fd6721 commit c1cd150

File tree

2 files changed

+247
-2
lines changed

2 files changed

+247
-2
lines changed

src/hooks/useRoomExp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export const useRoomExp = ({ onCountdown }: Props = {}) => {
4242
if (ejectAtExp && expUTCTimeStamp) {
4343
const expDate = new Date(expUTCTimeStamp * 1000);
4444
if (
45-
(newEjectDate.getTime() > 0 && expDate < newEjectDate) ||
46-
!newEjectDate.getTime()
45+
!newEjectDate.getTime() ||
46+
(newEjectDate.getTime() > 0 && expDate < newEjectDate)
4747
)
4848
newEjectDate = expDate;
4949
}

test/hooks/useRoomExp.test.tsx

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
/// <reference types="@types/jest" />
2+
3+
import DailyIframe, {
4+
DailyCall,
5+
DailyEvent,
6+
DailyEventObjectNoPayload,
7+
} from '@daily-co/daily-js';
8+
import { act, renderHook } from '@testing-library/react-hooks';
9+
import faker from 'faker';
10+
import React from 'react';
11+
12+
import { DailyProvider } from '../../src/DailyProvider';
13+
import { useRoom } from '../../src/hooks/useRoom';
14+
import { useRoomExp } from '../../src/hooks/useRoomExp';
15+
16+
jest.mock('../../src/DailyDevices', () => ({
17+
...jest.requireActual('../../src/DailyDevices'),
18+
DailyDevices: (({ children }) => <>{children}</>) as React.FC,
19+
}));
20+
jest.mock('../../src/DailyLiveStreaming', () => ({
21+
...jest.requireActual('../../src/DailyLiveStreaming'),
22+
DailyLiveStreaming: (({ children }) => <>{children}</>) as React.FC,
23+
}));
24+
jest.mock('../../src/DailyRecordings', () => ({
25+
...jest.requireActual('../../src/DailyRecordings'),
26+
DailyRecordings: (({ children }) => <>{children}</>) as React.FC,
27+
}));
28+
jest.mock('../../src/DailyMeeting', () => ({
29+
...jest.requireActual('../../src/DailyMeeting'),
30+
DailyMeeting: (({ children }) => <>{children}</>) as React.FC,
31+
}));
32+
jest.mock('../../src/DailyRoom', () => ({
33+
...jest.requireActual('../../src/DailyRoom'),
34+
DailyRoom: (({ children }) => <>{children}</>) as React.FC,
35+
}));
36+
jest.mock('../../src/hooks/useRoom');
37+
38+
const createWrapper =
39+
(callObject: DailyCall = DailyIframe.createCallObject()): React.FC =>
40+
({ children }) =>
41+
<DailyProvider callObject={callObject}>{children}</DailyProvider>;
42+
43+
describe('useRoomExp', () => {
44+
beforeAll(() => {
45+
jest.useFakeTimers();
46+
});
47+
48+
beforeEach(() => {
49+
jest.clearAllMocks();
50+
jest.clearAllTimers();
51+
});
52+
53+
afterAll(() => {
54+
jest.useRealTimers();
55+
});
56+
57+
it('returns null initially', async () => {
58+
const daily = DailyIframe.createCallObject();
59+
const { result, waitFor } = renderHook(() => useRoomExp(), {
60+
wrapper: createWrapper(daily),
61+
});
62+
await waitFor(() => {
63+
expect(result.current.ejectDate).toBeNull();
64+
});
65+
});
66+
67+
describe('eject_after_elapsed', () => {
68+
it('should return correct ejectDate based on joining-meeting date', async () => {
69+
let localJoinDate: Date;
70+
(useRoom as jest.Mock).mockImplementation(() => ({
71+
id: faker.datatype.uuid(),
72+
name: faker.random.word(),
73+
domainConfig: {},
74+
tokenConfig: {},
75+
config: {
76+
eject_after_elapsed: 60,
77+
eject_at_room_exp: false,
78+
exp: 0,
79+
},
80+
}));
81+
const daily = DailyIframe.createCallObject();
82+
const { result, waitFor } = renderHook(() => useRoomExp(), {
83+
wrapper: createWrapper(daily),
84+
});
85+
86+
await waitFor(() => {
87+
expect(result.current.ejectDate).toBeNull();
88+
});
89+
90+
act(() => {
91+
const action: DailyEvent = 'joining-meeting';
92+
const payload: DailyEventObjectNoPayload = {
93+
action,
94+
};
95+
// @ts-ignore
96+
daily.emit(action, payload);
97+
localJoinDate = new Date();
98+
});
99+
100+
await waitFor(() => {
101+
expect(result.current.ejectDate).toBeInstanceOf(Date);
102+
expect(result.current.ejectDate?.getTime()).toBe(
103+
localJoinDate.getTime() + 60000
104+
);
105+
});
106+
});
107+
it('should call onCountdown correctly during the countdown', () => {
108+
(useRoom as jest.Mock).mockImplementation(() => ({
109+
id: faker.datatype.uuid(),
110+
name: faker.random.word(),
111+
domainConfig: {},
112+
tokenConfig: {},
113+
config: {
114+
eject_after_elapsed: 3610,
115+
eject_at_room_exp: false,
116+
exp: 0,
117+
},
118+
}));
119+
120+
const daily = DailyIframe.createCallObject();
121+
const onCountdown = jest.fn();
122+
renderHook(() => useRoomExp({ onCountdown }), {
123+
wrapper: createWrapper(daily),
124+
});
125+
126+
act(() => {
127+
const action: DailyEvent = 'joining-meeting';
128+
const payload: DailyEventObjectNoPayload = {
129+
action,
130+
};
131+
// @ts-ignore
132+
daily.emit(action, payload);
133+
});
134+
135+
expect(onCountdown).toHaveBeenCalledTimes(0);
136+
137+
act(() => {
138+
jest.advanceTimersByTime(1000);
139+
});
140+
141+
expect(onCountdown).toHaveBeenCalledTimes(1);
142+
expect(onCountdown).toHaveBeenCalledWith({
143+
hours: 1,
144+
minutes: 0,
145+
seconds: 9,
146+
});
147+
148+
act(() => {
149+
jest.advanceTimersByTime(3540000);
150+
});
151+
152+
expect(onCountdown).toHaveBeenCalledTimes(3541);
153+
expect(onCountdown).toHaveBeenLastCalledWith({
154+
hours: 0,
155+
minutes: 1,
156+
seconds: 9,
157+
});
158+
159+
act(() => {
160+
jest.advanceTimersByTime(30000);
161+
});
162+
163+
expect(onCountdown).toHaveBeenCalledTimes(3571);
164+
expect(onCountdown).toHaveBeenLastCalledWith({
165+
hours: 0,
166+
minutes: 0,
167+
seconds: 39,
168+
});
169+
});
170+
});
171+
172+
describe('eject_at_room_exp', () => {
173+
it('should return correct ejectDate', async () => {
174+
(useRoom as jest.Mock).mockImplementation(() => ({
175+
id: faker.datatype.uuid(),
176+
name: faker.random.word(),
177+
domainConfig: {},
178+
tokenConfig: {},
179+
config: {
180+
eject_after_elapsed: null,
181+
eject_at_room_exp: true,
182+
exp: 4100760732, // 2099-12-12T12:12:12.000Z
183+
},
184+
}));
185+
const daily = DailyIframe.createCallObject();
186+
const { result, waitFor } = renderHook(() => useRoomExp(), {
187+
wrapper: createWrapper(daily),
188+
});
189+
190+
await waitFor(() => {
191+
expect(result.current.ejectDate).toBeInstanceOf(Date);
192+
expect(result.current.ejectDate).toEqual(
193+
new Date('2099-12-12T12:12:12.000Z')
194+
);
195+
});
196+
});
197+
it('should call onCountdown correctly during the countdown', () => {
198+
const now = new Date();
199+
const nowUnix = Math.ceil(now.getTime() / 1000);
200+
const exp = nowUnix + 60;
201+
202+
(useRoom as jest.Mock).mockImplementation(() => ({
203+
id: faker.datatype.uuid(),
204+
name: faker.random.word(),
205+
domainConfig: {},
206+
tokenConfig: {},
207+
config: {
208+
eject_after_elapsed: null,
209+
eject_at_room_exp: true,
210+
exp,
211+
},
212+
}));
213+
214+
const daily = DailyIframe.createCallObject();
215+
const onCountdown = jest.fn();
216+
renderHook(() => useRoomExp({ onCountdown }), {
217+
wrapper: createWrapper(daily),
218+
});
219+
220+
expect(onCountdown).toHaveBeenCalledTimes(0);
221+
222+
act(() => {
223+
jest.advanceTimersByTime(1000);
224+
});
225+
226+
expect(onCountdown).toHaveBeenCalledTimes(1);
227+
expect(onCountdown).toHaveBeenCalledWith({
228+
hours: 0,
229+
minutes: 0,
230+
seconds: 59,
231+
});
232+
233+
act(() => {
234+
jest.advanceTimersByTime(2000);
235+
});
236+
237+
expect(onCountdown).toHaveBeenCalledTimes(3);
238+
expect(onCountdown).toHaveBeenLastCalledWith({
239+
hours: 0,
240+
minutes: 0,
241+
seconds: 57,
242+
});
243+
});
244+
});
245+
});

0 commit comments

Comments
 (0)