Skip to content

Commit 5b78955

Browse files
committed
Coverage: login menu
"hinkyQAIssue" came from years ago; the solution isn't legal. use-navigate-by-key adjusted
1 parent 8e70fb9 commit 5b78955

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

src/app/layouts/default/header/menus/main-menu/dropdown/use-navigate-by-key.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import useDropdownContext from '../../dropdown-context';
22
import {isMobileDisplay} from '~/helpers/device';
3+
import { assertDefined } from '~/helpers/data';
34

45
function findNext(dropdownRef: React.MutableRefObject<HTMLDivElement | null>) {
56
const nextSib = document.activeElement?.nextElementSibling;
67

78
if (nextSib?.matches('a')) {
89
return nextSib as HTMLAnchorElement;
910
}
10-
const targets = Array.from(dropdownRef.current?.querySelectorAll('a') ?? []);
11+
const targets = Array.from(assertDefined(dropdownRef.current?.querySelectorAll('a')));
1112
const idx = targets.indexOf(document.activeElement as HTMLAnchorElement);
1213
const nextIdx = (idx + 1) % targets.length;
1314

1415
return targets[nextIdx];
1516
}
1617

17-
// eslint-disable-next-line complexity
1818
function findPrev(
1919
topRef: React.MutableRefObject<HTMLAnchorElement | null>,
2020
dropdownRef: React.MutableRefObject<HTMLDivElement | null>
@@ -24,7 +24,7 @@ function findPrev(
2424
if (prevSib?.matches('a')) {
2525
return prevSib as HTMLAnchorElement;
2626
}
27-
const targets = Array.from(dropdownRef.current?.querySelectorAll('a') ?? []);
27+
const targets = Array.from(assertDefined(dropdownRef.current?.querySelectorAll('a')));
2828
const idx = targets.indexOf(document.activeElement as HTMLAnchorElement);
2929

3030
if (idx === 0) {

src/app/layouts/default/header/menus/main-menu/login-menu/login-menu.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@ import linkHelper from '~/helpers/link';
77
function LoginLink() {
88
// It's not used directly, but loginLink changes when it does
99
useLocation();
10-
const addressHinkyQAIssue = React.useCallback(
11-
(e: React.MouseEvent<HTMLAnchorElement>) => {
12-
if (e.defaultPrevented) {
13-
e.defaultPrevented = false;
14-
}
15-
},
16-
[]
17-
);
1810

1911
return (
2012
<li className="login-menu nav-menu-item rightmost">
2113
<a
2214
href={linkHelper.loginLink()} className="pardotTrackClick"
23-
data-local="true" role="menuitem" onClick={addressHinkyQAIssue}
15+
data-local="true" role="menuitem"
2416
>
2517
Log in
2618
</a>

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

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,19 @@ describe('default layout', () => {
280280
});
281281
}
282282

283+
const myOpenStaxUser = {
284+
contact: {
285+
firstName: 'Roy',
286+
lastName: 'Johnson'
287+
}
288+
};
289+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
290+
const loggedInUser = {userModel: {id: 16249}, myOpenStaxUser} as any;
291+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
292+
const loggedOutUser = {} as any;
293+
294+
const spyUseUserContext = jest.spyOn(UUC, 'default').mockReturnValue(loggedOutUser);
295+
283296
beforeAll(() => {
284297
global.fetch = jest
285298
.fn()
@@ -328,6 +341,8 @@ describe('default layout', () => {
328341
await user.click(overlay as Element);
329342
expect(toggle.getAttribute('aria-expanded')).toBe('false');
330343

344+
spyUseUserContext.mockReturnValue(loggedInUser);
345+
331346
// close on Escape (but not on other keypress)
332347
await user.click(toggle);
333348
expect(toggle.getAttribute('aria-expanded')).toBe('true');
@@ -349,21 +364,16 @@ describe('default layout', () => {
349364

350365
fireEvent.focus(techMenu);
351366
fireEvent.keyDown(techMenu, {key: 'ArrowDown'});
367+
expect(document.activeElement?.textContent).toBe('OpenStax Assignable');
352368
fireEvent.keyDown(techMenu, {key: 'ArrowUp'});
369+
expect(document.activeElement?.textContent).toBe('Technology arrow');
353370
fireEvent.keyDown(techMenu, {key: 'ArrowRight'});
371+
expect(document.activeElement?.textContent).toBe('What we do arrow');
372+
fireEvent.keyDown(techMenu, {key: 'ArrowLeft'});
373+
expect(document.activeElement?.textContent).toBe('What we do arrow');
354374
expect(techMenu.getAttribute('aria-expanded')).toBe('false');
355375
});
356376
it('renders login menu', async () => {
357-
// Normally dynamically loaded, so needs its own test
358-
const myOpenStaxUser = {
359-
contact: {
360-
firstName: 'Roy',
361-
lastName: 'Johnson'
362-
}
363-
};
364-
365-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
366-
jest.spyOn(UUC, 'default').mockReturnValue({userModel: {id: 16249}, myOpenStaxUser} as any);
367377
render(<MemoryRouter initialEntries={['/webinars']}>
368378
<LoginMenu />
369379
</MemoryRouter>);
@@ -381,4 +391,26 @@ describe('default layout', () => {
381391
fireEvent.click(screen.getByText('Change route'));
382392
expect(toggleActive).toHaveBeenCalledWith(false);
383393
});
394+
it('renders login-menu options based on userModel', async () => {
395+
spyUseUserContext.mockReturnValue({
396+
myOpenStaxUser: {
397+
error: 'true'
398+
},
399+
// @ts-expect-error userModel missssing properties
400+
userModel: {
401+
instructorEligible: true,
402+
incompleteSignup: true,
403+
pendingInstructorAccess: true,
404+
emailUnverified: true
405+
}
406+
});
407+
render(<MemoryRouter initialEntries={['/webinars']}>
408+
<LoginMenu />
409+
</MemoryRouter>);
410+
screen.getByRole('link', {name: 'Account Profile'});
411+
screen.getByRole('link', {name: 'Request instructor access'});
412+
screen.getByRole('link', {name: 'Complete your profile'});
413+
screen.getByRole('link', {name: 'Pending instructor access'});
414+
screen.getByRole('link', {name: 'Verify your email address'});
415+
});
384416
});

0 commit comments

Comments
 (0)