Skip to content

Commit ee493ad

Browse files
UI - unit test cases for layout component
1 parent 7dd5d28 commit ee493ad

File tree

1 file changed

+84
-35
lines changed

1 file changed

+84
-35
lines changed

frontend/src/pages/layout/Layout.test.tsx

Lines changed: 84 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ Object.assign(global, {
1414
})
1515

1616
describe('Layout', () => {
17+
18+
beforeEach(() => {
19+
jest.clearAllMocks();
20+
21+
Object.assign(navigator, {
22+
clipboard: {
23+
writeText: jest.fn(),
24+
},
25+
});
26+
});
27+
1728
const mockDispatch = jest.fn()
1829

1930
const mockAppState = {
@@ -63,6 +74,8 @@ describe('Layout', () => {
6374
})
6475

6576
it('copies URL to clipboard when the copy button is clicked', async () => {
77+
const mockWriteText = jest.spyOn(navigator.clipboard, 'writeText').mockResolvedValue();
78+
6679
renderWithContext(mockAppState)
6780

6881
const shareButton = screen.getByText('Share')
@@ -72,13 +85,82 @@ describe('Layout', () => {
7285
fireEvent.click(copyButton)
7386

7487
await waitFor(() => {
75-
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(window.location.href)
88+
//expect(navigator.clipboard.writeText).toHaveBeenCalledWith(window.location.href)
89+
expect(mockWriteText).toHaveBeenCalledWith(window.location.href);
7690
})
7791

7892
expect(screen.getByText('Copied URL')).toBeInTheDocument()
7993
})
8094

95+
it('should trigger handleCopyClick when Enter key is pressed', () => {
96+
// Mock the clipboard function
97+
const mockWriteText = jest.spyOn(navigator.clipboard, 'writeText').mockResolvedValue();
98+
99+
// Render the component
100+
renderWithContext(mockAppState)
101+
102+
const shareButton = screen.getByText('Share')
103+
fireEvent.click(shareButton)
104+
105+
// Find the copy button container
106+
const copyButton = screen.getByLabelText('Copy')
107+
108+
// Simulate keydown event for the Enter key
109+
fireEvent.keyDown(copyButton, { key: 'Enter', code: 'Enter' });
110+
111+
// Assert that the clipboard writeText function is called
112+
expect(mockWriteText).toHaveBeenCalledWith(window.location.href);
113+
expect(screen.getByText('Copied URL')).toBeInTheDocument();
114+
});
115+
116+
it('should trigger handleCopyClick when Space key is pressed', () => {
117+
// Mock the clipboard function
118+
const mockWriteText = jest.spyOn(navigator.clipboard, 'writeText').mockResolvedValue();
119+
120+
// Render the component
121+
renderWithContext(mockAppState)
122+
123+
const shareButton = screen.getByText('Share')
124+
fireEvent.click(shareButton)
125+
126+
// Find the copy button container
127+
const copyButton = screen.getByLabelText('Copy')
128+
129+
// Simulate keydown event for the Space key
130+
fireEvent.keyDown(copyButton, { key: ' ', code: 'Space' });
131+
132+
// Assert that the clipboard writeText function is called
133+
expect(mockWriteText).toHaveBeenCalledWith(window.location.href);
134+
expect(screen.getByText('Copied URL')).toBeInTheDocument();
135+
});
136+
137+
it('should not trigger handleCopyClick for other keys', () => {
138+
// Mock the clipboard function
139+
const mockWriteText = jest.spyOn(navigator.clipboard, 'writeText').mockResolvedValue();
140+
141+
// Render the component
142+
renderWithContext(mockAppState)
143+
144+
const shareButton = screen.getByText('Share')
145+
fireEvent.click(shareButton)
146+
147+
// Find the copy button container
148+
const copyButton = screen.getByLabelText('Copy')
149+
150+
// Simulate keydown event for an unrelated key
151+
fireEvent.keyDown(copyButton, { key: 'Tab', code: 'Tab' });
152+
153+
// Assert that the clipboard writeText function is not called
154+
expect(mockWriteText).not.toHaveBeenCalled();
155+
});
156+
81157
it('toggles history visibility when history button is clicked', async () => {
158+
159+
jest.spyOn(window, 'location', 'get').mockReturnValue({
160+
...window.location,
161+
pathname: '/generate',
162+
});
163+
82164
renderWithContext(mockAppState)
83165

84166
const historyButton = screen.getByText('Show template history')
@@ -99,40 +181,7 @@ describe('Layout', () => {
99181

100182
expect(screen.queryByText('Share')).not.toBeInTheDocument()
101183
})
102-
it('shows the share button', () => {
103-
renderWithContext(mockAppState)
104-
expect(screen.getByText('Share')).toBeInTheDocument()
105-
})
106-
107-
it('opens share panel when share button is clicked', () => {
108-
renderWithContext(mockAppState)
109-
fireEvent.click(screen.getByText('Share'))
110-
expect(screen.getByText('Share the web app')).toBeInTheDocument()
111-
})
112-
113-
it('copies URL to clipboard when copy button is clicked', () => {
114-
Object.assign(navigator, {
115-
clipboard: {
116-
writeText: jest.fn()
117-
}
118-
})
119-
120-
renderWithContext(mockAppState)
121-
fireEvent.click(screen.getByText('Share'))
122-
fireEvent.click(screen.getByRole('button', { name: /copy/i }))
123-
124-
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(window.location.href)
125-
expect(screen.getByText('Copied URL')).toBeInTheDocument()
126-
})
127-
128-
it('closes the share panel when dismissed', () => {
129-
renderWithContext(mockAppState)
130-
fireEvent.click(screen.getByText('Share'))
131-
fireEvent.click(screen.getByLabelText('Close'))
132-
133-
expect(screen.queryByText('Share the web app')).not.toBeInTheDocument()
134-
})
135-
184+
136185
it('toggles history button visibility based on path', () => {
137186
renderWithContext(mockAppState)
138187
expect(screen.queryByText('Show template history')).not.toBeInTheDocument()

0 commit comments

Comments
 (0)