Skip to content

Commit f820044

Browse files
committed
Coverage of default/shared
1 parent 98d6f27 commit f820044

File tree

3 files changed

+103
-58
lines changed

3 files changed

+103
-58
lines changed

src/app/layouts/default/shared.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type BannerInfo = {
1515
type StickyDataRaw = {
1616
start: string;
1717
expires: string;
18-
emergency_expires?: string;
18+
emergency_expires: string;
1919
show_popup: boolean;
2020
};
2121

@@ -62,7 +62,7 @@ function getMode(stickyData: StickyDataRaw | null): 'emergency' | 'popup' | 'ban
6262
return null;
6363
}
6464

65-
const expireDate = new Date(stickyData.emergency_expires ?? '');
65+
const expireDate = new Date(stickyData.emergency_expires);
6666
const useEmergency = stickyData.emergency_expires && Date.now() < expireDate.getTime();
6767

6868
if (useEmergency) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"html_message":"Millions of students are counting on OpenStax this fall. Want to help? Give monthly and open up learning they can trust — today, tomorrow, every semester.","link_text":"Give now!","link_url":"https://riceconnect.rice.edu/donation/support-openstax-banner","banner_thumbnail":"https://assets.openstax.org/oscms-prodcms/media/original_images/Blue_White_Cute_Simple_Doodle_iOS_Icon.png"}]

test/src/layouts/default/default.test.tsx

Lines changed: 100 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import fundraiserData from './data/fundraiser.json';
99
import footerData from './data/footer.json';
1010
import oxmenuData from './data/osmenu.json';
1111
import giveTodayData from './data/give-today.json';
12+
import giveBannerData from './data/givebanner.json';
13+
import * as CF from '~/helpers/cms-fetch';
1214
import '@testing-library/jest-dom';
1315

1416
// Mock external dependencies
@@ -33,10 +35,6 @@ let mockPathname = {pathname: '/test-path'};
3335
jest.mock('react-router-dom', () => ({
3436
useLocation: () => mockPathname
3537
}));
36-
// jest.mock('~/layouts/default/microsurvey-popup/microsurvey-popup', () => ({
37-
// __esModule: true,
38-
// default: () => <></>
39-
// }));
4038

4139
const mockUseSharedDataContext = jest.fn().mockReturnValue({stickyFooterState: [false, () => undefined]});
4240

@@ -62,6 +60,22 @@ Reflect.defineProperty(window, 'localStorage', {
6260
});
6361

6462
const user = userEvent.setup();
63+
const basicImplementation = (path: string) => {
64+
if (path === 'sticky/') {
65+
return Promise.resolve({
66+
...stickyData
67+
});
68+
}
69+
if (path === 'snippets/givebanner') {
70+
return Promise.resolve(giveBannerData);
71+
}
72+
if (path === 'give-today') {
73+
return Promise.resolve(giveTodayData);
74+
}
75+
console.info('**** Whoops', path);
76+
return Promise.resolve([]);
77+
};
78+
const mockCmsFetch = jest.spyOn(CF, 'default').mockImplementation(basicImplementation);
6579

6680
describe('Layouts Default TypeScript Conversions', () => {
6781
beforeEach(() => {
@@ -126,6 +140,88 @@ describe('Layouts Default TypeScript Conversions', () => {
126140
render(<TestComponent />);
127141
expect(screen.getByTestId('sticky-data')).toHaveTextContent('no data');
128142
});
143+
test('useStickyData hook handles emergency mode', async () => {
144+
mockCmsFetch.mockImplementation(
145+
(path: string) => {
146+
if (path === 'sticky/') {
147+
/* eslint-disable camelcase */
148+
return Promise.resolve({
149+
...stickyData,
150+
emergency_expires: '2044-05-31T23:00:00Z'
151+
});
152+
/* eslint-enable camelcase */
153+
}
154+
return basicImplementation(path);
155+
}
156+
);
157+
const TestComponent = () => {
158+
const data = useStickyData();
159+
160+
return (
161+
<div data-testid="sticky-data">
162+
{data ? data.mode : 'no data'}
163+
</div>
164+
);
165+
};
166+
167+
render(<TestComponent />);
168+
expect(await screen.findByTestId('sticky-data')).toHaveTextContent('emergency');
169+
mockCmsFetch.mockImplementation(basicImplementation);
170+
});
171+
test('useStickyData hook handles microdonation not active', async () => {
172+
mockCmsFetch.mockImplementation(
173+
(path: string) => {
174+
if (path === 'sticky/') {
175+
return Promise.resolve({
176+
...stickyData,
177+
expires: '2024-05-31T23:00:00Z'
178+
});
179+
}
180+
return basicImplementation(path);
181+
}
182+
);
183+
const TestComponent = () => {
184+
const data = useStickyData();
185+
186+
return (
187+
<div data-testid="sticky-data">
188+
{data && data.mode === null ? 'mode is null' : 'no data'}
189+
</div>
190+
);
191+
};
192+
193+
render(<TestComponent />);
194+
expect(await screen.findByTestId('sticky-data')).toHaveTextContent('mode is null');
195+
mockCmsFetch.mockImplementation(basicImplementation);
196+
});
197+
test('useStickyData hook handles show_popup', async () => {
198+
mockCmsFetch.mockImplementation(
199+
(path: string) => {
200+
if (path === 'sticky/') {
201+
/* eslint-disable camelcase */
202+
return Promise.resolve({
203+
...stickyData,
204+
show_popup: true
205+
});
206+
/* eslint-enable camelcase */
207+
}
208+
return basicImplementation(path);
209+
}
210+
);
211+
const TestComponent = () => {
212+
const data = useStickyData();
213+
214+
return (
215+
<div data-testid="sticky-data">
216+
{data && data.mode === 'popup' ? 'mode is popup' : 'no data'}
217+
</div>
218+
);
219+
};
220+
221+
render(<TestComponent />);
222+
expect(await screen.findByTestId('sticky-data')).toHaveTextContent('mode is popup');
223+
mockCmsFetch.mockImplementation(basicImplementation);
224+
});
129225
});
130226

131227
// describe('MenuItem component TypeScript props', () => {
@@ -321,55 +417,3 @@ describe('default layout', () => {
321417
expect(toggle.getAttribute('aria-expanded')).toBe('false');
322418
});
323419
});
324-
325-
// Integration tests for complex TypeScript interactions
326-
// describe('TypeScript Integration Tests', () => {
327-
// test('Complex type interactions work correctly', async () => {
328-
// // Test that complex types work together without compilation errors
329-
// type ComplexData = {
330-
// id: number;
331-
// metadata: {
332-
// created: string;
333-
// modified?: string;
334-
// };
335-
// items: Array<{
336-
// name: string;
337-
// value: number;
338-
// }>;
339-
// };
340-
341-
// const testData: ComplexData = {
342-
// id: 1,
343-
// metadata: {
344-
// created: '2024-01-01'
345-
// },
346-
// items: [
347-
// {name: 'item1', value: 100},
348-
// {name: 'item2', value: 200}
349-
// ]
350-
// };
351-
352-
// expect(testData.id).toBe(1);
353-
// expect(testData.items).toHaveLength(2);
354-
// expect(testData.metadata.modified).toBeUndefined();
355-
// });
356-
357-
// test('Event handler types are properly defined', () => {
358-
// type EventHandlerProps = {
359-
// onClick: (event: React.MouseEvent<HTMLButtonElement>) => void;
360-
// onKeyDown: (event: React.KeyboardEvent<HTMLButtonElement>) => void;
361-
// };
362-
363-
// const TestComponent = ({onClick, onKeyDown}: EventHandlerProps) => (
364-
// <button onClick={onClick} onKeyDown={onKeyDown}>
365-
// Test Button
366-
// </button>
367-
// );
368-
369-
// const mockClick = jest.fn();
370-
// const mockKeyDown = jest.fn();
371-
372-
// render(<TestComponent onClick={mockClick} onKeyDown={mockKeyDown} />);
373-
// expect(screen.getByRole('button')).toBeInTheDocument();
374-
// });
375-
// });

0 commit comments

Comments
 (0)