Skip to content

Commit b857580

Browse files
authored
DOP-6261 URL is opened into the targeted browsing context (#1556)
1 parent 1e65935 commit b857580

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

src/components/Widgets/MarkdownWidget/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ const CopyPageMarkdownButton = ({ className, slug }: CopyPageMarkdownButtonProps
9797

9898
const viewMarkdown = () => {
9999
if (!markdownAddress) return;
100-
window.location.href = markdownAddress;
100+
// There is a preference to open in a new tab, the default target is _blank
101+
window.open(markdownAddress);
101102
};
102103

103104
const askQuestion = () => {

tests/unit/CopyPageMarkdownButton.test.tsx

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { render, fireEvent, waitFor, act } from '@testing-library/react';
33
import * as Gatsby from 'gatsby';
44
import * as ReachRouter from '@gatsbyjs/reach-router';
55
import CopyPageMarkdownButton from '../../src/components/Widgets/MarkdownWidget';
6+
import { removeTrailingSlash } from '../../src/utils/remove-trailing-slash';
7+
8+
const TEST_URL = 'http://localhost:8000/tutorial/foo/';
69

710
const renderCopyMarkdownButton = (props = {}) => render(<CopyPageMarkdownButton {...props} />);
811

@@ -31,8 +34,6 @@ jest.mock('../../src/context/chatbot-context', () => ({
3134
ChatbotProvider: ({ children }: { children: React.ReactNode }) => children,
3235
}));
3336

34-
const TEST_URL = 'http://localhost:8000/tutorial/foo/';
35-
3637
// Mock clipboard
3738
Object.defineProperty(global.navigator, 'clipboard', {
3839
value: {
@@ -41,6 +42,9 @@ Object.defineProperty(global.navigator, 'clipboard', {
4142
writable: true,
4243
});
4344

45+
// Mock the window open API
46+
Object.defineProperty(global.window, 'open', { configurable: true, writable: true, value: jest.fn(() => ({} as any)) });
47+
4448
describe('Copy markdown button', () => {
4549
beforeEach(() => {
4650
// Mock location
@@ -92,6 +96,37 @@ describe('Copy markdown button', () => {
9296
});
9397
});
9498

99+
it('the URL is opened into the targeted browsing context', async () => {
100+
mockedFetch.mockResolvedValue({
101+
ok: true,
102+
text: () => Promise.resolve('# Markdown content'),
103+
});
104+
105+
const viewInMarkdown = /view in markdown/i;
106+
let getByText: any;
107+
let getByLabelText: any;
108+
109+
await act(async () => {
110+
const result = renderCopyMarkdownButton();
111+
getByLabelText = result.getByLabelText;
112+
getByText = result.getByText;
113+
});
114+
115+
// Click the menu action
116+
act(() => fireEvent.click(getByLabelText('More options')));
117+
118+
// Wait for the menu to appear and click "View in markdown"
119+
await waitFor(() => {
120+
expect(getByText(viewInMarkdown)).toBeInTheDocument();
121+
});
122+
123+
// Click the action
124+
act(() => fireEvent.click(getByText(viewInMarkdown)));
125+
126+
expect(window.open).toHaveBeenCalledTimes(1);
127+
expect(window.open).toHaveBeenCalledWith(`${removeTrailingSlash(TEST_URL)}.md`);
128+
});
129+
95130
it('triggers error when hitting a 404 page', async () => {
96131
// Mock fetch response
97132
mockedFetch.mockResolvedValue({
@@ -129,18 +164,30 @@ describe('Copy markdown button', () => {
129164
});
130165

131166
it('opens chatbot with pre-filled message question about the current page', async () => {
167+
mockedFetch.mockResolvedValue({
168+
ok: true,
169+
text: () => Promise.resolve('# Markdown content'),
170+
});
171+
132172
// Render component with a slug prop
133-
const { getByLabelText, getByText } = renderCopyMarkdownButton({ slug: 'tutorial/foo' });
173+
let getByLabelText: any;
174+
let getByText: any;
175+
176+
await act(async () => {
177+
const result = renderCopyMarkdownButton({ slug: 'tutorial/foo' });
178+
getByLabelText = result.getByLabelText;
179+
getByText = result.getByText;
180+
});
134181

135182
// Click the dropdown trigger (arrow button) to open the menu
136-
fireEvent.click(getByLabelText('More options'));
183+
await act(() => fireEvent.click(getByLabelText('More options')));
137184

138185
// Wait for menu to appear and click "Ask a Question"
139186
await waitFor(() => {
140187
expect(getByText('Ask a Question')).toBeInTheDocument();
141188
});
142189

143-
fireEvent.click(getByText('Ask a Question'));
190+
await act(() => fireEvent.click(getByText('Ask a Question')));
144191

145192
// Verify that setText was called with the correct message (our new implementation)
146193
expect(mockSetText).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)