Skip to content

Commit

Permalink
test(suiteheader): Changing some tests to make sure they run with a k…
Browse files Browse the repository at this point in the history
…nown `window.location.href` so that the existence of `originHref` query paramaterer in the logout routes can be validated.
  • Loading branch information
Marcelo Blechner committed Aug 22, 2022
1 parent 9dc3b73 commit c5df330
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import IdleLogoutConfirmationModal from './IdleLogoutConfirmationModal';

const commonProps = {
routes: {
logout: 'https://www.ibm.com',
logoutInactivity: 'https://www.ibm.com',
logout: 'https://www.ibm.com/logout',
logoutInactivity: 'https://www.ibm.com/inactivity',
domain: '',
},
idleTimeoutData: { countdown: 10, timeout: 10, cookieName: '_user_inactivity_timeout' },
Expand All @@ -20,14 +20,22 @@ const commonProps = {
const TIME_INTERVAL = 1000;

describe('IdleLogoutConfirmationModal', () => {
const originHref = 'https://ibm.com';
const expectedLogoutRoute = `${commonProps.routes.logout}?originHref=${encodeURIComponent(
originHref
)}`;
const expectedLogoutInactivityRoute = `${
commonProps.routes.logoutInactivity
}?originHref=${encodeURIComponent(originHref)}`;

let originalWindowLocation;
let originalWindowDocumentCookie;
beforeEach(() => {
jest.useFakeTimers();
originalWindowLocation = { ...window.location };
originalWindowDocumentCookie = window.document.cookie;
delete window.location;
window.location = { href: '' };
window.location = { href: 'https://ibm.com' };
window.open = jest.fn();
});

Expand Down Expand Up @@ -110,6 +118,23 @@ describe('IdleLogoutConfirmationModal', () => {
expect(mockOnStayLoggedIn).toHaveBeenCalled();
});
it('user clicks Log Out on the idle logout confirmation dialog', async () => {
render(<IdleLogoutConfirmationModal {...commonProps} onRouteChange={async () => true} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
writable: true,
value: `${commonProps.idleTimeoutData.cookieName}=${Date.now() - TIME_INTERVAL}`,
});
act(() => {
jest.runOnlyPendingTimers();
});
const modalLogoutButton = within(screen.getByTestId('idle-logout-confirmation')).getByText(
SuiteHeaderI18N.en.sessionTimeoutModalLogoutButton
);
await userEvent.click(modalLogoutButton);
expect(window.location.href).toBe(expectedLogoutRoute);
});
it('user clicks Log Out on the idle logout confirmation dialog (no originHref)', async () => {
window.location = { href: '' };
render(<IdleLogoutConfirmationModal {...commonProps} onRouteChange={async () => true} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
Expand Down Expand Up @@ -142,6 +167,21 @@ describe('IdleLogoutConfirmationModal', () => {
expect(window.location.href).not.toBe(commonProps.routes.logout);
});
it('idle user waits for the logout confirmation dialog countdown to finish', async () => {
render(<IdleLogoutConfirmationModal {...commonProps} onRouteChange={async () => true} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
writable: true,
value: `${commonProps.idleTimeoutData.cookieName}=${Date.now() - TIME_INTERVAL}`,
});
// Go to the future by a little more than commonProps.idleTimeoutData.countdown seconds
MockDate.set(Date.now() + (commonProps.idleTimeoutData.countdown + 1) * TIME_INTERVAL);
await act(async () => {
await jest.runOnlyPendingTimers();
});
await waitFor(() => expect(window.location.href).toBe(expectedLogoutInactivityRoute));
});
it('idle user waits for the logout confirmation dialog countdown to finish (no originHref)', async () => {
window.location = { href: '' };
render(<IdleLogoutConfirmationModal {...commonProps} onRouteChange={async () => true} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
Expand Down Expand Up @@ -179,7 +219,7 @@ describe('IdleLogoutConfirmationModal', () => {
await act(async () => {
await jest.runOnlyPendingTimers();
});
expect(window.location.href).toBe(commonProps.routes.logout);
expect(window.location.href).toBe(expectedLogoutRoute);
});
it('user has logged out in another tab (but no redirect)', async () => {
render(<IdleLogoutConfirmationModal {...commonProps} onRouteChange={async () => false} />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,21 @@ const idleTimeoutDataProp = {
};

describe('SuiteHeader', () => {
const originHref = 'https://ibm.com';
const expectedLogoutRoute = `${
adminPageCommonProps.routes.logout
}?originHref=${encodeURIComponent(originHref)}`;
const expectedLogoutInactivityRoute = `${
adminPageCommonProps.routes.logoutInactivity
}?originHref=${encodeURIComponent(originHref)}`;
let originalWindowLocation;
let originalWindowDocumentCookie;
beforeEach(() => {
jest.useFakeTimers();
originalWindowLocation = { ...window.location };
originalWindowDocumentCookie = window.document.cookie;
delete window.location;
window.location = { href: '' };
window.location = { href: 'https://ibm.com' };
window.open = jest.fn();
});

Expand Down Expand Up @@ -245,14 +252,20 @@ describe('SuiteHeader', () => {
expect(screen.getByRole('presentation')).not.toHaveClass('is-visible');
});
it('clicks logout link', async () => {
render(<SuiteHeader {...adminPageCommonProps} />);
await userEvent.click(screen.getAllByRole('button', { name: 'Log out' })[0]);
expect(window.location.href).toBe(expectedLogoutRoute);
});
it('clicks logout link (no originHref)', async () => {
window.location = { href: '' };
render(<SuiteHeader {...adminPageCommonProps} />);
await userEvent.click(screen.getAllByRole('button', { name: 'Log out' })[0]);
expect(window.location.href).toBe(adminPageCommonProps.routes.logout);
});
it('clicks logout link (but no redirect)', async () => {
render(<SuiteHeader {...adminPageCommonProps} onRouteChange={async () => false} />);
await userEvent.click(screen.getAllByRole('button', { name: 'Log out' })[0]);
expect(window.location.href).not.toBe(adminPageCommonProps.routes.logout);
expect(window.location.href).not.toBe(expectedLogoutRoute);
});
it('Legacy (single-workspace only) admin button should no longer be visible', async () => {
render(<SuiteHeader {...adminPageCommonProps} isAdminView />);
Expand Down Expand Up @@ -438,6 +451,23 @@ describe('SuiteHeader', () => {
expect(mockOnStayLoggedIn).toHaveBeenCalled();
});
it('user clicks Log Out on the idle logout confirmation dialog', async () => {
render(<SuiteHeader {...adminPageCommonProps} idleTimeoutData={idleTimeoutDataProp} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
writable: true,
value: `${idleTimeoutDataProp.cookieName}=${Date.now() - 1000}`,
});
act(() => {
jest.runOnlyPendingTimers();
});
const modalLogoutButton = within(screen.getByTestId('idle-logout-confirmation')).getByText(
SuiteHeaderI18N.en.sessionTimeoutModalLogoutButton
);
await userEvent.click(modalLogoutButton);
expect(window.location.href).toBe(expectedLogoutRoute);
});
it('user clicks Log Out on the idle logout confirmation dialog (no originHref)', async () => {
window.location = { href: '' };
render(<SuiteHeader {...adminPageCommonProps} idleTimeoutData={idleTimeoutDataProp} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
Expand Down Expand Up @@ -473,9 +503,24 @@ describe('SuiteHeader', () => {
SuiteHeaderI18N.en.sessionTimeoutModalLogoutButton
);
await userEvent.click(modalLogoutButton);
expect(window.location.href).not.toBe(adminPageCommonProps.routes.logout);
expect(window.location.href).not.toBe(expectedLogoutRoute);
});
it('idle user waits for the logout confirmation dialog countdown to finish', async () => {
render(<SuiteHeader {...adminPageCommonProps} idleTimeoutData={idleTimeoutDataProp} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
writable: true,
value: `${idleTimeoutDataProp.cookieName}=${Date.now() - 1000}`,
});
// Go to the future by a little more than idleTimeoutDataProp.countdown seconds
MockDate.set(Date.now() + (idleTimeoutDataProp.countdown + 1) * 1000);
await act(async () => {
await jest.runOnlyPendingTimers();
});
await waitFor(() => expect(window.location.href).toBe(expectedLogoutInactivityRoute));
});
it('idle user waits for the logout confirmation dialog countdown to finish (no originHref)', async () => {
window.location = { href: '' };
render(<SuiteHeader {...adminPageCommonProps} idleTimeoutData={idleTimeoutDataProp} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
Expand Down Expand Up @@ -509,9 +554,7 @@ describe('SuiteHeader', () => {
await act(async () => {
await jest.runOnlyPendingTimers();
});
await waitFor(() =>
expect(window.location.href).not.toBe(adminPageCommonProps.routes.logoutInactivity)
);
await waitFor(() => expect(window.location.href).not.toBe(expectedLogoutInactivityRoute));
});
it('renders Walkme', async () => {
render(<SuiteHeader {...adminPageCommonProps} walkmePath="/some/test/path" walkmeLang="en" />);
Expand Down
53 changes: 49 additions & 4 deletions packages/react/src/components/SuiteHeader/SuiteHeader.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,21 @@ const idleTimeoutDataProp = {
};

describe('SuiteHeader', () => {
const originHref = 'https://ibm.com';
const expectedLogoutRoute = `${commonProps.routes.logout}?originHref=${encodeURIComponent(
originHref
)}`;
const expectedLogoutInactivityRoute = `${
commonProps.routes.logoutInactivity
}?originHref=${encodeURIComponent(originHref)}`;
let originalWindowLocation;
let originalWindowDocumentCookie;
beforeEach(() => {
jest.useFakeTimers();
originalWindowLocation = { ...window.location };
originalWindowDocumentCookie = window.document.cookie;
delete window.location;
window.location = { href: '' };
window.location = { href: originHref };
window.open = jest.fn();
});

Expand Down Expand Up @@ -150,14 +157,20 @@ describe('SuiteHeader', () => {
expect(screen.getByRole('presentation')).not.toHaveClass('is-visible');
});
it('clicks logout link', async () => {
render(<SuiteHeader {...commonProps} />);
await userEvent.click(screen.getAllByRole('button', { name: 'Log out' })[0]);
expect(window.location.href).toBe(expectedLogoutRoute);
});
it('clicks logout link (no originHref)', async () => {
window.location = { href: '' };
render(<SuiteHeader {...commonProps} />);
await userEvent.click(screen.getAllByRole('button', { name: 'Log out' })[0]);
expect(window.location.href).toBe(commonProps.routes.logout);
});
it('clicks logout link (but no redirect)', async () => {
render(<SuiteHeader {...commonProps} onRouteChange={async () => false} />);
await userEvent.click(screen.getAllByRole('button', { name: 'Log out' })[0]);
expect(window.location.href).not.toBe(commonProps.routes.logout);
expect(window.location.href).not.toBe(expectedLogoutRoute);
});
it('admin link from admin view takes you to navigator route', async () => {
render(<SuiteHeader {...commonProps} isAdminView />);
Expand Down Expand Up @@ -357,6 +370,23 @@ describe('SuiteHeader', () => {
expect(mockOnStayLoggedIn).toHaveBeenCalled();
});
it('user clicks Log Out on the idle logout confirmation dialog', async () => {
render(<SuiteHeader {...commonProps} idleTimeoutData={idleTimeoutDataProp} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
writable: true,
value: `${idleTimeoutDataProp.cookieName}=${Date.now() - 1000}`,
});
act(() => {
jest.runOnlyPendingTimers();
});
const modalLogoutButton = within(screen.getByTestId('idle-logout-confirmation')).getByText(
SuiteHeaderI18N.en.sessionTimeoutModalLogoutButton
);
await userEvent.click(modalLogoutButton);
expect(window.location.href).toBe(expectedLogoutRoute);
});
it('user clicks Log Out on the idle logout confirmation dialog (no originHref)', async () => {
window.location = { href: '' };
render(<SuiteHeader {...commonProps} idleTimeoutData={idleTimeoutDataProp} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
Expand Down Expand Up @@ -392,9 +422,24 @@ describe('SuiteHeader', () => {
SuiteHeaderI18N.en.sessionTimeoutModalLogoutButton
);
await userEvent.click(modalLogoutButton);
expect(window.location.href).not.toBe(commonProps.routes.logout);
expect(window.location.href).not.toBe(expectedLogoutRoute);
});
it('idle user waits for the logout confirmation dialog countdown to finish', async () => {
render(<SuiteHeader {...commonProps} idleTimeoutData={idleTimeoutDataProp} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
writable: true,
value: `${idleTimeoutDataProp.cookieName}=${Date.now() - 1000}`,
});
// Go to the future by a little more than idleTimeoutDataProp.countdown seconds
MockDate.set(Date.now() + (idleTimeoutDataProp.countdown + 1) * 1000);
await act(async () => {
await jest.runOnlyPendingTimers();
});
await waitFor(() => expect(window.location.href).toBe(expectedLogoutInactivityRoute));
});
it('idle user waits for the logout confirmation dialog countdown to finish (no originHref)', async () => {
window.location = { href: '' };
render(<SuiteHeader {...commonProps} idleTimeoutData={idleTimeoutDataProp} />);
// Simulate a timestamp cookie that is in the past
Object.defineProperty(window.document, 'cookie', {
Expand Down Expand Up @@ -426,7 +471,7 @@ describe('SuiteHeader', () => {
await act(async () => {
await jest.runOnlyPendingTimers();
});
await waitFor(() => expect(window.location.href).not.toBe(commonProps.routes.logoutInactivity));
await waitFor(() => expect(window.location.href).not.toBe(expectedLogoutInactivityRoute));
});
it('renders Walkme', async () => {
render(<SuiteHeader {...commonProps} walkmePath="/some/test/path" walkmeLang="en" />);
Expand Down

0 comments on commit c5df330

Please sign in to comment.