Skip to content

Commit

Permalink
Merge pull request #227 from cozy/feat/remove-enzyme
Browse files Browse the repository at this point in the history
Remove enzyme
  • Loading branch information
zatteo authored Nov 28, 2024
2 parents bddc86f + 5833125 commit 406a553
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 364 deletions.
4 changes: 0 additions & 4 deletions enzyme.setup.js

This file was deleted.

2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
moduleFileExtensions: ['js', 'jsx', 'json'],
setupFilesAfterEnv: ['<rootDir>/enzyme.setup.js', '<rootDir>jest.setup.js'],
setupFilesAfterEnv: ['<rootDir>jest.setup.js'],
testPathIgnorePatterns: ['@bitwarden/jslib', 'transpiled/'],
transformIgnorePatterns: ['node_modules/(?!(cozy-ui))'],
watchPathIgnorePatterns: ['node_modules'],
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@semantic-release/git": "7.0.18",
"@semantic-release/npm": "5.3.5",
"@testing-library/react": "11.2.7",
"@testing-library/react-hooks": "3.7.0",
"@testing-library/react-hooks": "8.0.1",
"babel-loader": "8.2.4",
"babel-plugin-inline-react-svg": "1.1.2",
"babel-plugin-mock-imports": "1.2.0",
Expand All @@ -53,13 +53,11 @@
"cozy-flags": "2.8.6",
"cozy-intent": "2.13.1",
"cozy-ui": "93.1.1",
"enzyme": "3.11.0",
"enzyme-adapter-react-16": "1.15.7",
"enzyme-to-json": "3.4.0",
"eslint-config-cozy-app": "1.6.0",
"jest": "26.2.2",
"react": "16.14.0",
"react-dom": "16.14.0",
"react-test-renderer": "16.14.0",
"semantic-release": "^15.13.24"
},
"files": [
Expand Down
77 changes: 45 additions & 32 deletions src/components/VaultContext.spec.jsx
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')
})
})

Expand Down
Loading

0 comments on commit 406a553

Please sign in to comment.