-
Notifications
You must be signed in to change notification settings - Fork 1
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 #227 from cozy/feat/remove-enzyme
Remove enzyme
- Loading branch information
Showing
5 changed files
with
76 additions
and
364 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,71 +1,84 @@ | ||
import React from 'react' | ||
import { mount } from 'enzyme' | ||
import { EventEmitter } from 'events' | ||
import { | ||
VaultContext, | ||
VaultProvider, | ||
withVaultClient, | ||
useVaultClient | ||
} from './VaultContext' | ||
import WebVaultClient from '../WebVaultClient' | ||
import { render, waitFor } from '@testing-library/react' | ||
import { renderHook } from '@testing-library/react-hooks' | ||
|
||
jest.mock('../WebVaultClient') | ||
|
||
const createFakeVaultClient = () => { | ||
const vaultClient = new EventEmitter() | ||
|
||
vaultClient.on('lock', () => { | ||
vaultClient.locked = true | ||
}) | ||
vaultClient.on('unlock', () => { | ||
vaultClient.locked = false | ||
}) | ||
vaultClient.isLocked = () => vaultClient.locked | ||
|
||
return vaultClient | ||
} | ||
|
||
describe('VaultProvider', () => { | ||
it('should rerender when the locked state changes', async () => { | ||
const ChildComponent = jest.fn() | ||
const ChildComponent = jest.fn(() => null) | ||
const vaultClient = createFakeVaultClient() | ||
|
||
const component = mount( | ||
<VaultProvider instance={'[email protected]'}> | ||
render( | ||
<VaultProvider vaultClient={vaultClient} instance={'[email protected]'}> | ||
<VaultContext.Consumer>{ChildComponent}</VaultContext.Consumer> | ||
</VaultProvider> | ||
) | ||
|
||
const vaultClient = component.state('vaultClient') | ||
expect(ChildComponent).toHaveBeenCalledWith({ | ||
vaultClient, | ||
locked: true | ||
}) | ||
|
||
expect(ChildComponent).toHaveBeenCalledWith({ vaultClient, locked: true }) | ||
vaultClient.emit('unlock') | ||
|
||
vaultClient.unlock() | ||
|
||
await new Promise(resolve => | ||
setTimeout(() => { | ||
expect(ChildComponent).toHaveBeenCalledTimes(3) | ||
expect(ChildComponent).toHaveBeenCalledWith({ | ||
vaultClient, | ||
locked: false | ||
}) | ||
resolve() | ||
await waitFor(() => { | ||
expect(ChildComponent).toHaveBeenCalledWith({ | ||
vaultClient, | ||
locked: false | ||
}) | ||
) | ||
}) | ||
|
||
vaultClient.lock() | ||
vaultClient.emit('lock') | ||
|
||
await new Promise(resolve => | ||
setTimeout(() => { | ||
expect(ChildComponent).toHaveBeenCalledTimes(4) | ||
expect(ChildComponent).toHaveBeenCalledWith({ | ||
vaultClient, | ||
locked: true | ||
}) | ||
resolve() | ||
await waitFor(() => { | ||
expect(ChildComponent).toHaveBeenCalledWith({ | ||
vaultClient, | ||
locked: true | ||
}) | ||
) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('withVaultClient', () => { | ||
it('should inject vaultClient as a prop', () => { | ||
const ChildComponent = () => <div /> | ||
it('should inject vaultClient as a prop', async () => { | ||
const ChildComponent = jest.fn(() => null) | ||
|
||
const ChildWithClient = withVaultClient(ChildComponent) | ||
|
||
const component = mount( | ||
render( | ||
<VaultProvider instance={'[email protected]'}> | ||
<ChildWithClient /> | ||
</VaultProvider> | ||
) | ||
|
||
const child = component.find(ChildComponent) | ||
expect(child.prop('vaultClient')).toBeDefined() | ||
// Check if the vault is correctly injected as a prop | ||
const propsPassedToChildComponent = Object.keys( | ||
ChildComponent.mock.calls[0][0] | ||
) | ||
expect(propsPassedToChildComponent).toContain('vaultClient') | ||
}) | ||
}) | ||
|
||
|
Oops, something went wrong.