opinion-ateTDD_personal/src/store/restaurants.spec.js

127 lines
3.7 KiB
JavaScript

import { createStore, applyMiddleware } from "redux";
import thunk from 'redux-thunk';
import restaurantReducer from './restaurants/reducers';
import { loadRestaurants } from "./restaurants/actions";
describe('restaurants', () =>
{
describe('initially', () =>
{
let store;
beforeEach(() =>
{
const initialState = {};
store = createStore(
restaurantReducer,
initialState,
applyMiddleware(thunk),
);
});
it('does not have the loading flag set', () =>
{
expect(store.getState().loading).toEqual(false);
});
it('does not have the error flag set', () =>
{
expect(store.getState().loadError).toEqual(false);
});
});
describe('loadRestaurants Action', () =>
{
describe('When Loading Succeeds', () =>
{
const records = [
{ id: 1, name: 'Sushi Place' },
{ id: 2, name: 'Pizza Place' }
];
let store;
beforeEach(() =>
{
const api = {
loadRestaurants: () => Promise.resolve(records),
};
const initialState = {
records: [],
};
store = createStore(
restaurantReducer,
initialState,
applyMiddleware(
thunk.withExtraArgument(api),
),
);
return store.dispatch(loadRestaurants());
})
it('stores the restaurants', async () =>
{
expect(store.getState().records).toEqual(records)
});
it('clears the loading flag', () =>
{
expect(store.getState().loading).toEqual(false);
});
});
describe('While Loading', () =>
{
let store;
beforeEach(() =>
{
const api = {
loadRestaurants: () => new Promise(() => { }),
}
const initialState = { loadError: true };
store = createStore(
restaurantReducer,
initialState,
applyMiddleware(
thunk.withExtraArgument(api),
),
);
store.dispatch(loadRestaurants());
});
it('sets a loading flag', () =>
{
expect(store.getState().loading).toEqual(true);
});
it('clears the error flag', () =>
{
expect(store.getState().loadError).toEqual(false);
});
});
describe('When Loading Fails', () =>
{
let store;
beforeEach(() =>
{
const api = {
loadRestaurants: () => Promise.reject(),
};
const initialState = {};
store = createStore(
restaurantReducer,
initialState,
applyMiddleware(
thunk.withExtraArgument(api),
),
);
return store.dispatch(loadRestaurants());
});
it('sets an error flag', () =>
{
expect(store.getState().loadError).toEqual(true);
});
it('clears the loading flag', () =>
{
expect(store.getState().loading).toEqual(false);
});
});
});
});