Skip to content

Instantly share code, notes, and snippets.

View carlosble's full-sized avatar

Carlos Ble carlosble

View GitHub Profile
@carlosble
carlosble / billing.spec.js
Created April 5, 2017 18:27
Populate the store with server data
it("renders the invoice coming from server", (done) => {
const invoice = {
reference: '0001',
amount: '10.00',
};
spyNotifier.slowOperationFinished = () => {
let billRow = page.find(BillsTable).first().find(BillRow);
expect(billRow.length).toBe(1);
done();
};
@carlosble
carlosble / billing.spec.js
Created April 5, 2017 18:43
Finer test, checking the store
it("populates the store with server data", (done) => {
const invoice = {
reference: '0001',
amount: '10.00',
};
store.subscribe(() => {
expect(store.getState().billing.invoices[0]).toEqual(invoice);
done();
});
stubApi.getInvoices = () =>{
@carlosble
carlosble / logout.spec.js
Created April 5, 2017 18:52
Asserting agains the store
beforeEach(function () {
spyRouter = { push: jest.fn() };
initialState = {profile: {id: '123456789H'}};
store = configureStore(initialState);
});
it("clears store's state on logout", (done) => {
expect(store.getState().profile.id).toEqual(initialState.profile.id);
store.subscribe(() => {
expect(store.getState().profile.id).toEqual('');
@carlosble
carlosble / addressPage.spec.js
Created April 5, 2017 19:18
Testing components in isolation with Enzyme's shallow
beforeEach(()=> {
form = shallow(<AddressForm
address={{}}
actions={actions}
/>);
});
it('contains exactly one email input for every address email', () => {
let address = addressBuilder().withEmails([email0, email1]).build();
form.setProps({address: address});
@carlosble
carlosble / addressPage.spec.js
Created April 5, 2017 19:22
Spying on the action to test component in isolation
it('deletes email when button is clicked', () => {
let address = addressBuilder().withEmails([email0, email1]).build();
form.setProps({address: address});
actions.updateEmails = jest.fn((value) => {
expect(value).toEqual(email1);
});
const buttons = form.find(inputByClass(deleteEmailCss));
simulateClick(buttons.first());
@carlosble
carlosble / addressActions.js
Last active April 5, 2017 22:05
Asynchronous action processing data before sendin to the server
function saveAddress(address, serverApi) {
return function(dispatch, getState) {
dispatch({
type: types.SAVE_ADDRESS,
address: address
});
let cleanAddress = getState().address;
return serverApi.saveAddress(cleanAddress).then((json) => {
if (areEmailsDifferent(cleanAddress, address)) {
let response = Object.assign({}, json);
@carlosble
carlosble / profileActions.js
Created April 5, 2017 22:04
Asynchronous action populating the store with data from the server
function loadProfile(serverApi) {
return function (dispatch) {
return serverApi.getProfile().then((json) => {
return dispatch({
type: types.GET_PROFILE,
profile: json
});
});
};
}
@carlosble
carlosble / tests.js
Last active June 7, 2017 22:06
Simulating changes in location
let stubApi = {};
let store;
let stubRouter = {
location: {
search: '',
query: {
toDay: '',
fromDay: ''
}
},
export const createPage = (serverApi, mapState) => {
return connect(
(state) => {
let mapping = {
report: state.report
};
if (typeof(mapState) === 'function'){
return Object.assign(mapping, mapState(state));
}
return mapping;
@carlosble
carlosble / tests.js
Created June 7, 2017 22:08
Eventually expect
it ('populates store with data returned from server', (done) => {
stubApiClientToReturn({username: 'carlos'});
simulateSearchFormSubmit({username: 'car'});
expectStoredUsersToContainUsername('carlos', done);
});
function expectStoredUsersToContainUsername(username, done){
store.subscribe(eventuallyExpect(() => {