Skip to content

Instantly share code, notes, and snippets.

@wahengchang
Last active October 29, 2024 18:28
Show Gist options
  • Save wahengchang/108ca55981f6600c252ad0cb9d4c518f to your computer and use it in GitHub Desktop.
Save wahengchang/108ca55981f6600c252ad0cb9d4c518f to your computer and use it in GitHub Desktop.
Unit test, mocking components
import { InstallCom } from 'installComponent' //installed by npm
import UserCom from './userComponent'
export class ShareCom extends Component {
render() {
return (
<div>
<InstallCom para1='title1'/>
<UserCom para2='title2' />
</div>
)
}
}
import {ShareCom} from '../somewhere/ShareCom'
jest.mock('../somewhere/UserCom', () => ()=> <div id="mockUserCom">mockUserCom</div>)
jest.mock('installComponent', () => ({
InstallCom: 'mockInstallCom'
}))
describe('ShareCom', () => {
it('should return correct component', () => {
const wrapper = mount(
<ShareCom
something={something}
/>
)
expect(wrapper.find('mockInstallCom').length).toEqual(1)
expect(wrapper.find('#mockUserCom').length).toEqual(1)
})
})
@videni
Copy link

videni commented Jun 25, 2018

not working

@LissetteIbnz
Copy link

Great!

@vm137
Copy link

vm137 commented Apr 10, 2019

Great! works for me ))

@shiun75
Copy link

shiun75 commented Sep 9, 2019

👍 Thank you! Works for me

@g-yoskowitch
Copy link

i whant understand when i write this mock :const mockValidateObject = jest.fn(({ entityName, object, configData }) => {
if (entityName instanceof String) {
return true;
} else {
return false;
}
})
jest.mock('../../../modules/validation', () => {
return {
validateObject: mockValidateObject,
}
})and when i write this mock:jest.mock('../../../modules/validation', () => ({
validateObject: (() => {
return false
})
}))What does it depend on??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment