Skip to content

Commit 240e431

Browse files
committed
Test coverage
1 parent df4cd64 commit 240e431

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/app/components/shell/router-helpers/use-link-handler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ export type TrackedMouseEvent = React.MouseEvent<HTMLAnchorElement> & {
2020
function handleExternalLink(href: Location['href'], el: HTMLElement) {
2121
if (el.dataset.local === 'true') {
2222
// REX books open in the current window; track them
23-
window.location.href = href;
23+
window.location.assign(href);
2424
} else {
2525
const newWindow = window.open(href, '_blank');
2626

2727
if (newWindow === null) {
28-
window.location.href = href;
28+
window.location.assign(href);
2929
}
3030
}
3131
}
@@ -45,7 +45,7 @@ export default function useLinkHandler() {
4545
const stripped = linkHelper.stripOpenStaxDomain(path);
4646

4747
if (stripped.startsWith('http')) {
48-
window.location.href = stripped;
48+
window.location.assign(stripped);
4949
} else {
5050
navigate(stripped, state);
5151
}

test/src/components/shell/use-link-handler.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('use-link-handler', () => {
6060
<InnerComponent {...props} />
6161
</MemoryRouter>
6262
);
63+
const saveError = console.error;
6364

6465
afterEach(jest.resetAllMocks);
6566

@@ -98,10 +99,29 @@ describe('use-link-handler', () => {
9899
(useNavigate as jest.Mock).mockReturnValue(navigate);
99100

100101
render(<Component />);
102+
console.error = jest.fn();
101103
await user.click(screen.getByRole('link'));
104+
expect(console.error).toHaveBeenCalled();
105+
console.error = saveError;
102106
expect(notPrevented).not.toBeCalled();
103107
expect(navigate).toBeCalledWith('whatever', {x: 0, y: 0});
104108
});
109+
it('Changes window location when stripping does nothing', async () => {
110+
setPortalPrefix('/portal');
111+
const el = document.createElement('a');
112+
113+
jest.spyOn(linkHelper, 'validUrlClick').mockReturnValue(el);
114+
jest.spyOn(linkHelper, 'stripOpenStaxDomain').mockReturnValue(
115+
'http://whatever'
116+
);
117+
118+
render(<Component />);
119+
console.error = jest.fn();
120+
await user.click(screen.getByRole('link'));
121+
expect(console.error).toHaveBeenCalled();
122+
console.error = saveError;
123+
expect(notPrevented).not.toBeCalled();
124+
});
105125
it('calls piTracker if available', async () => {
106126
setPortalPrefix('/portal');
107127
const navigate = jest.fn();
@@ -141,7 +161,10 @@ describe('use-link-handler', () => {
141161
(useNavigate as jest.Mock).mockReturnValue(navigate);
142162

143163
render(<Component />);
164+
console.error = jest.fn();
144165
await user.click(screen.getByRole('link'));
166+
expect(console.error).toHaveBeenCalled();
167+
console.error = saveError;
145168
expect(notPrevented).not.toBeCalled();
146169
expect(navigate).not.toBeCalled();
147170
});
@@ -163,7 +186,10 @@ describe('use-link-handler', () => {
163186
(useNavigate as jest.Mock).mockReturnValue(navigate);
164187

165188
render(<Component />);
189+
console.error = jest.fn();
166190
await user.click(screen.getByRole('link'));
191+
expect(console.error).toHaveBeenCalled();
192+
console.error = saveError;
167193
expect(notPrevented).not.toBeCalled();
168194
expect(navigate).not.toBeCalled();
169195
});
@@ -212,5 +238,6 @@ describe('use-link-handler', () => {
212238
);
213239
await user.click(screen.getByRole('link'));
214240
expect(console.error).toBeCalled();
241+
console.error = saveError;
215242
});
216243
});

test/src/data/all-books.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"subject_search_description": "This is math search meta"
2929
}
3030
],
31-
"content_warning_text": null,
31+
"content_warning_text": "Warning text",
3232
"cover_url": "https://assets.openstax.org/oscms-dev/media/documents/prealgebra_1.svg"
3333
},
3434
{

0 commit comments

Comments
 (0)