e2e test passing

This commit is contained in:
2024-04-24 16:05:19 -04:00
parent 5db0d0afc1
commit e390640938
7 changed files with 95 additions and 4 deletions

13
src/api.js Normal file
View File

@ -0,0 +1,13 @@
import axios from 'axios';
const client = axios.create({
baseURL: 'https://api.outsidein.dev/Sy0NsAyaS8uuURS1zyCDp3Fzxcpg25iw',
});
const api = {
async loadRestaurants(){
const response = await client.get('/restaurants');
return response.data;
},
};
export default api;

View File

@ -1,7 +1,13 @@
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';
import api from '../api';
const store = createStore(rootReducer, applyMiddleware(thunk));
const store = createStore(
rootReducer,
applyMiddleware(
thunk.withExtraArgument(api),
),
);
export default store;

View File

@ -0,0 +1,32 @@
import { createStore, applyMiddleware } from "redux";
import thunk from 'redux-thunk';
import restaurantReducer from './restaurants/reducers';
import { loadRestaurants } from "./restaurants/actions";
describe('restaurants', () => {
describe('loadRestaurants action', () => {
it('stores the restaurants', async () => {
const records = [
{id: 1, name: 'Sushi Place'},
{id: 2, name: 'Pizza Place'}
];
const api = {
loadRestaurants: () => Promise.resolve(records),
};
const initialState = {
records: [],
};
const store = createStore(
restaurantReducer,
initialState,
applyMiddleware(
thunk.withExtraArgument(api),
),
);
await store.dispatch(loadRestaurants());
expect(store.getState().records).toEqual(records)
});
});
});

View File

@ -1 +1,11 @@
export const loadRestaurants = () => () => {};
export const STORE_RESTAURANTS = 'STORE_RESTAURANTS';
export const loadRestaurants = () => async (dispatch, getState, api) => {
const records = await api.loadRestaurants();
dispatch(storeRestaurants(records));
};
const storeRestaurants = records => ({
type: STORE_RESTAURANTS,
records
});

View File

@ -1,7 +1,13 @@
import { combineReducers } from 'redux';
import { STORE_RESTAURANTS } from './actions';
function records() {
return[];
function records(state = [], action) {
switch(action.type) {
case STORE_RESTAURANTS:
return action.records;
default:
return state;
}
}
export default combineReducers({