-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #689 from cozy/fix-logout
- Loading branch information
Showing
5 changed files
with
307 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- "8" | ||
- "10" | ||
env: | ||
global: | ||
# BUNDLESIZE_GITHUB_TOKEN | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React from 'react' | ||
import { Provider } from 'react-redux' | ||
|
||
import I18n from 'cozy-ui/react/I18n' | ||
import { createStore } from 'lib/store' | ||
import enLocale from 'locales/en.json' | ||
import { render, screen, fireEvent, act } from '@testing-library/react' | ||
import { Drawer } from './Drawer' | ||
|
||
const sleep = duration => new Promise(resolve => setTimeout(resolve, duration)) | ||
|
||
const fakeStore = createStore() | ||
|
||
const Wrapper = ({ children }) => { | ||
return ( | ||
<Provider store={fakeStore}> | ||
<I18n dictRequire={() => enLocale} lang="en"> | ||
{children} | ||
</I18n> | ||
</Provider> | ||
) | ||
} | ||
|
||
describe('bar', () => { | ||
describe('logout', () => { | ||
const findLogoutButton = () => { | ||
return screen.getByText('Sign out') | ||
} | ||
const setup = ({ onLogOut, logOut }) => { | ||
render( | ||
<Wrapper> | ||
<Drawer | ||
toggleSupport={jest.fn()} | ||
logOut={logOut} | ||
onLogOut={onLogOut} | ||
/> | ||
</Wrapper> | ||
) | ||
} | ||
|
||
const clickLogout = () => { | ||
const logoutButton = findLogoutButton() | ||
fireEvent( | ||
logoutButton, | ||
new MouseEvent('click', { | ||
bubbles: true, | ||
cancelable: true | ||
}) | ||
) | ||
} | ||
|
||
it('should await the onLogOut', async () => { | ||
let prom | ||
const callOrder = [] | ||
const logOut = jest.fn().mockImplementation(() => { | ||
callOrder.push('logOut') | ||
}) | ||
const onLogOut = jest.fn().mockImplementation(async () => { | ||
prom = sleep(100) | ||
callOrder.push('onLogOut') | ||
await prom | ||
}) | ||
|
||
setup({ logOut, onLogOut }) | ||
|
||
act(() => { | ||
clickLogout() | ||
}) | ||
|
||
expect(logOut).not.toHaveBeenCalled() | ||
await prom | ||
await sleep(0) | ||
expect(logOut).toHaveBeenCalled() | ||
expect(onLogOut).toHaveBeenCalled() | ||
expect(callOrder).toEqual(['onLogOut', 'logOut']) | ||
}) | ||
|
||
it('should work if onLogOut has not been passed', () => { | ||
const logOut = jest.fn() | ||
setup({ logOut }) | ||
act(() => { | ||
clickLogout() | ||
}) | ||
expect(logOut).toHaveBeenCalled() | ||
}) | ||
|
||
it('should work if onLogOut does not return a promise', () => { | ||
const logOut = jest.fn() | ||
const onLogOut = jest.fn() | ||
setup({ logOut, onLogOut }) | ||
act(() => { | ||
clickLogout() | ||
}) | ||
expect(logOut).toHaveBeenCalled() | ||
expect(onLogOut).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.