Skip to content

Instantly share code, notes, and snippets.

@maxx-coffee
Created November 9, 2017 19:10
Show Gist options
  • Save maxx-coffee/afaf6810b8f1736ab5d402de87b988d8 to your computer and use it in GitHub Desktop.
Save maxx-coffee/afaf6810b8f1736ab5d402de87b988d8 to your computer and use it in GitHub Desktop.
Dependency injection via javascript
// because you cannot mock window or document we will need to mock these exports
export const getDocument = () => (document);
export const getWindow = () => (window);
import React from 'react';
import { Modal } from 'react-bootstrap';
import { getWindow } from 'LIB/documentHelper';
import textFormatter from './TextFormatter';
export default (props) => (
<Modal>
<Modal.Header>
<Modal.Title>Modal heading</Modal.Title>
</Modal.Header>
<Modal.Body>
<p>{getWindow().isAdmin ? textFormatter('you are an admin') : textFormatter('boiii you lame')}</p>
</Modal.Body>
<Modal.Footer>
<span>Close</span>
</Modal.Footer>
</Modal>
);
import { expect, React, Sinon } from 'TEST/TestHelper';
import { shallow } from 'enzyme';
import MuhComponent from './index';
import * as BootStrap from 'react-bootstrap';
import * as documentHelpers from 'LIB/documentHelper';
import * as textFormatter from './TextFormatter';
describe('mocking imports', () => {
describe('rendering a mock for the bootstrap component', () => {
let DefaultModal;
before(() => {
DefaultModal = BootStrap.Modal;
BootStrap.Modal = (props) => (
<div>
<p id="render_me">this should render</p>
{props.children}
</div>
);
});
after(() => {
BootStrap.Modal = DefaultModal;
});
it('should render a mock for bootstraps modal', () => {
const wrapper = shallow(<MuhComponent />);
expect(wrapper.dive().find('#render_me').length).to.eq(1);
});
})
describe('not rendering a mock for the bootstrap component', () => {
it('should render a mock for bootstraps modal', () => {
const wrapper = shallow(<MuhComponent />);
expect(wrapper.dive().find('#render_me').length).to.eq(0);
});
});
//
context('when the user is an admin', () => {
before(() => {
Sinon.stub(documentHelpers, 'getWindow', () => ({isAdmin: true}));
});
after(() => {
documentHelpers.getWindow.restore();
});
it('should render the admin text', () => {
const wrapper = shallow(<MuhComponent />);
expect(wrapper.find(BootStrap.Modal.Body).dive().text()).to.eq('you are an admin');
})
});
//
// context('when the user is not an admin', () => {
// before(() => {
// Sinon.stub(documentHelpers, 'getWindow', () => ({isAdmin: false}));
// });
//
// after(() => {
// documentHelpers.getWindow.restore();
// });
//
// it('should render the admin text', () => {
// const wrapper = shallow(<MuhComponent />);
// expect(wrapper.find(BootStrap.Modal.Body).dive().text()).to.eq('boiii you lame');
// })
// })
//
describe('mocking a default', () => {
let DefaultFormatter;
before(() => {
DefaultFormatter = textFormatter.default;
textFormatter.default = (text) => text.toUpperCase();
});
after(() => {
textFormatter.default = DefaultFormatter;
});
it('should mock the formatter', () => {
const wrapper = shallow(<MuhComponent />);
expect(wrapper.find(BootStrap.Modal.Body).dive().text()).to.eq('boiii you lame'.toUpperCase());
})
})
});
export default (text) => text;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment