Skip to content

Commit 833848d

Browse files
committed
fix(context): Test context provider
1 parent 71ac889 commit 833848d

File tree

3 files changed

+142
-3
lines changed

3 files changed

+142
-3
lines changed

cypress/e2e/DataViewEvents.spec.cy.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
describe('Test the Data view docs page', () => {
2+
3+
it('displays a table and opens detail', () => {
4+
const ouiaId = 'ContextExample';
5+
6+
cy.visit('http://localhost:8006/extensions/data-view/context');
7+
8+
cy.get(`[data-ouia-component-id="${ouiaId}-th-0"]`).contains('Repositories');
9+
cy.get(`[data-ouia-component-id="${ouiaId}-th-4"]`).contains('Last commit');
10+
11+
cy.get(`[data-ouia-component-id="${ouiaId}-td-0-0"]`).contains('one');
12+
cy.get(`[data-ouia-component-id="${ouiaId}-td-4-4"]`).contains('five - 5');
13+
cy.get(`[data-ouia-component-id="${ouiaId}-td-7-4"]`).should('not.exist');
14+
15+
// click the first row
16+
cy.get(`[data-ouia-component-id="${ouiaId}-tr-0"]`).first().click();
17+
cy.get(`[data-ouia-component-id="detail-drawer"]`).should('exist');
18+
cy.get(`[data-ouia-component-id="detail-drawer-title"]`).contains('Detail of repository one');
19+
cy.get(`[data-ouia-component-id="detail-drawer-close-btn"]`).should('be.visible');
20+
21+
// click the first row again
22+
cy.get(`[data-ouia-component-id="${ouiaId}-tr-0"]`).first().click({ force: true });
23+
cy.get(`[data-ouia-component-id="detail-drawer-title"]`).should('not.be.visible');
24+
25+
// click the second row
26+
cy.get(`[data-ouia-component-id="${ouiaId}-tr-1"]`).first().click();
27+
cy.get(`[data-ouia-component-id="detail-drawer"]`).should('be.visible');
28+
cy.get(`[data-ouia-component-id="detail-drawer-title"]`).contains('Detail of repository one - 2');
29+
30+
// click the close button
31+
cy.get(`[data-ouia-component-id="detail-drawer-close-btn"]`).first().click({ force: true });
32+
cy.get(`[data-ouia-component-id="detail-drawer-title"]`).should('not.be.visible');
33+
})
34+
});

packages/module/patternfly-docs/content/extensions/data-view/examples/Context/EventsExample.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ const RepositoryDetail: React.FunctionComponent<RepositoryDetailProps> = ({ sele
5151
return (
5252
<DrawerPanelContent>
5353
<DrawerHead>
54-
<Title className="pf-v5-u-mb-md" headingLevel="h2">
54+
<Title className="pf-v5-u-mb-md" headingLevel="h2" ouiaId="detail-drawer-title">
5555
Detail of repository {selectedRepo?.name}
5656
</Title>
5757
<Text>Branches: {selectedRepo?.branches}</Text>
5858
<Text>Pull requests: {selectedRepo?.prs}</Text>
5959
<Text>Workspaces: {selectedRepo?.workspaces}</Text>
6060
<Text>Last commit: {selectedRepo?.lastCommit}</Text>
6161
<DrawerActions>
62-
<DrawerCloseButton onClick={() => setSelectedRepo(undefined)} />
62+
<DrawerCloseButton onClick={() => setSelectedRepo(undefined)} data-ouia-component-id="detail-drawer-close-btn"/>
6363
</DrawerActions>
6464
</DrawerHead>
6565
</DrawerPanelContent>
@@ -117,7 +117,7 @@ export const BasicExample: React.FunctionComponent = () => {
117117

118118
return (
119119
<DataViewProvider>
120-
<Drawer isExpanded={Boolean(selectedRepo)} onExpand={() => drawerRef.current?.focus()}>
120+
<Drawer isExpanded={Boolean(selectedRepo)} onExpand={() => drawerRef.current?.focus()} data-ouia-component-id="detail-drawer" >
121121
<DrawerContent
122122
panelContent={<RepositoryDetail selectedRepo={selectedRepo} setSelectedRepo={setSelectedRepo} />}
123123
>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import React from 'react';
2+
import { render, fireEvent } from '@testing-library/react';
3+
import { DataViewContext, DataViewProvider, EventTypes } from './DataViewContext';
4+
5+
let id = 0;
6+
7+
beforeAll(() => {
8+
Object.defineProperty(global, 'crypto', {
9+
value: {
10+
randomUUID: jest.fn(() => `mocked-uuid-${id++}`),
11+
},
12+
});
13+
});
14+
15+
describe('DataViewContext', () => {
16+
test('should provide context value and allow subscriptions', () => {
17+
const callback = jest.fn();
18+
19+
const TestComponent = () => {
20+
const { subscribe, trigger } = React.useContext(DataViewContext);
21+
22+
React.useEffect(() => {
23+
const unsubscribe = subscribe(EventTypes.rowClick, callback);
24+
return () => unsubscribe();
25+
// eslint-disable-next-line react-hooks/exhaustive-deps
26+
}, []);
27+
28+
return (
29+
<button onClick={() => trigger(EventTypes.rowClick, 'some payload')}>Trigger</button>
30+
);
31+
};
32+
33+
const { getByText } = render(
34+
<DataViewProvider>
35+
<TestComponent />
36+
</DataViewProvider>
37+
);
38+
39+
fireEvent.click(getByText('Trigger'));
40+
expect(callback).toHaveBeenCalledWith('some payload');
41+
});
42+
43+
test('should handle unsubscribing correctly', () => {
44+
const callback = jest.fn();
45+
46+
const TestComponent = () => {
47+
const { subscribe, trigger } = React.useContext(DataViewContext);
48+
49+
React.useEffect(() => {
50+
const unsubscribe = subscribe(EventTypes.rowClick, callback);
51+
unsubscribe();
52+
// eslint-disable-next-line react-hooks/exhaustive-deps
53+
}, []);
54+
55+
return (
56+
<button onClick={() => trigger(EventTypes.rowClick, 'some payload')}>Trigger</button>
57+
);
58+
};
59+
60+
const { getByText } = render(
61+
<DataViewProvider>
62+
<TestComponent />
63+
</DataViewProvider>
64+
);
65+
66+
fireEvent.click(getByText('Trigger'));
67+
68+
expect(callback).not.toHaveBeenCalled();
69+
});
70+
71+
test('should handle multiple subscriptions and trigger events correctly', () => {
72+
const callback1 = jest.fn();
73+
const callback2 = jest.fn();
74+
75+
const TestComponent = () => {
76+
const { subscribe, trigger } = React.useContext(DataViewContext);
77+
78+
React.useEffect(() => {
79+
const unsubscribe1 = subscribe(EventTypes.rowClick, callback1);
80+
const unsubscribe2 = subscribe(EventTypes.rowClick, callback2);
81+
82+
return () => {
83+
unsubscribe1();
84+
unsubscribe2();
85+
};
86+
// eslint-disable-next-line react-hooks/exhaustive-deps
87+
}, []);
88+
89+
return (
90+
<button onClick={() => trigger(EventTypes.rowClick, 'some payload')}>Trigger</button>
91+
);
92+
};
93+
94+
const { getByText } = render(
95+
<DataViewProvider>
96+
<TestComponent />
97+
</DataViewProvider>
98+
);
99+
100+
fireEvent.click(getByText('Trigger'));
101+
102+
expect(callback1).toHaveBeenCalledWith('some payload');
103+
expect(callback2).toHaveBeenCalledWith('some payload');
104+
});
105+
});

0 commit comments

Comments
 (0)