This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // App.js | |
| function App() { | |
| const stateAPIStatus = useLoadFoodData(); | |
| return ( | |
| <div className="food-app"> | |
| <header> | |
| <h1>Ordux</h1> | |
| </header> | |
| <Message status={stateAPIStatus} /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| import { Provider } from 'react-redux'; | |
| import { render, screen, waitForElementToBeRemoved } from '@testing-library/react'; | |
| import '@testing-library/jest-dom/extend-expect'; | |
| import App from './App'; | |
| import { createReduxStore } from './redux'; | |
| describe('Test App', () => { | |
| function renderApp(store = createReduxStore(), props = {}) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test('only show veg food when veg filter is applied', () => { | |
| // arrange | |
| render(<App {...props} />); | |
| // act | |
| fireEvent.click(screen.getByRole('checkbox', { name: /Veg Only/i })); | |
| // assert | |
| expect(screen.queryByText(/Sausage McMuffin/i)).toBe(null); | |
| expect(screen.getByText(/Mushroom Pizza/i)).toBeInTheDocument(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // redux.js | |
| const enableReduxDevTools = window.__REDUX_DEVTOOLS_EXTENSION__?.(); | |
| export function createReduxStore() { | |
| const store = createStore(foodReducer, enableReduxDevTools); | |
| return store; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| case ACTIONS.LOAD_MENU: { | |
| const { menu } = action.payload; | |
| const menuById = {}; | |
| menu.forEach((item) => { | |
| menuById[item.id] = item; | |
| }); | |
| const allMenuId = menu.map((item) => item.id); | |
| const vegMenuId = menu | |
| .filter((item) => item.diet === "veg") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function foodReducer(state = initialState, action) { | |
| switch (action.type) { | |
| case ACTIONS.CHANGE_DIET: { | |
| const { diet } = state; | |
| const newDiet = diet === 'veg' ? 'all' : 'veg'; | |
| return { | |
| ...state, | |
| diet: newDiet, | |
| cartByIds: {}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const initialState = { | |
| diet: 'all', | |
| menuById: {}, | |
| menuIdList: { | |
| all: [], | |
| veg: [], | |
| }, | |
| cartByIds: {}, | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const ACTIONS = { | |
| CHANGE_DIET: 'CHANGE_DIET', | |
| LOAD_MENU: 'LOAD_MENU', | |
| ADD_TO_CART: 'ADD_TO_CART', | |
| REMOVE_FROM_CART: 'REMOVE_FROM_CART', | |
| }; |