-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from Hello-Kitchen/65-refactorisation-fooddeta…
…iljs-function-logic add: refactorisation of the detail logic, handled now in sub-functions
- Loading branch information
Showing
2 changed files
with
143 additions
and
159 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,125 @@ | ||
import React from 'react'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import FoodDetail from '../../../Components/FoodElem/FoodDetail/FoodDetail'; | ||
|
||
describe('FoodDetail Component', () => { | ||
const mockSetOrderDetails = jest.fn(); | ||
const name = "Accompagnement"; | ||
const data = ["Frite", "Salade", "Riz", "Pâte", "Semoule"]; | ||
const orderDetails = {details: [], sups: []}; | ||
|
||
beforeEach(() => { | ||
const defaultProps = { | ||
name: 'Accompagnement', | ||
data: ['Frite', 'Salade', 'Riz', "Pâte"], | ||
multiple: true, | ||
orderDetails: { | ||
details: [], | ||
sups: [], | ||
}, | ||
setOrderDetails: mockSetOrderDetails, | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('renders FoodDetail', () => { | ||
render( | ||
<FoodDetail | ||
name={name} | ||
data={data} | ||
multiple={false} | ||
orderDetails={orderDetails} | ||
setOrderDetails={mockSetOrderDetails} | ||
/> | ||
); | ||
|
||
expect(screen.getByText(name)).toBeInTheDocument(); | ||
data.forEach(item => { | ||
expect(screen.getByText(item)).toBeInTheDocument(); | ||
test('renders component', () => { | ||
const { getByText } = render(<FoodDetail {...defaultProps} />); | ||
|
||
expect(getByText('Accompagnement')).toBeInTheDocument(); | ||
defaultProps.data.forEach((item) => { | ||
expect(getByText(item)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test('updates an option when multiple = false', () => { | ||
render( | ||
<FoodDetail | ||
name={name} | ||
data={data} | ||
multiple={false} | ||
orderDetails={orderDetails} | ||
setOrderDetails={mockSetOrderDetails} | ||
/> | ||
); | ||
test('initializes data with orderDetails', () => { | ||
const customProps = { | ||
...defaultProps, | ||
orderDetails: { | ||
details: [{name: 'Accompagnement', list: ['Riz']}], | ||
sups: [], | ||
}, | ||
}; | ||
|
||
const selectedButton = screen.getByText("Frite").closest('div'); | ||
fireEvent.click(selectedButton.querySelector('button')); | ||
const { getByText } = render(<FoodDetail {...customProps} />); | ||
|
||
const selectedButton = getByText('Riz').closest('div'); | ||
expect(selectedButton).toHaveClass('bg-kitchen-food-detail-selected'); | ||
}); | ||
|
||
test('handles selection', () => { | ||
const singleChoiceProps = {...defaultProps, multiple: false}; | ||
const { getByText } = render(<FoodDetail {...singleChoiceProps} />); | ||
|
||
const selectedButton = getByText('Riz').closest('button'); | ||
const secondButton = getByText('Frite').closest('button'); | ||
|
||
fireEvent.click(selectedButton); | ||
expect(mockSetOrderDetails).toHaveBeenCalledWith({ | ||
details: [{ name: name, list: ["Frite"] }], | ||
sups: [] | ||
details: [{name: 'Accompagnement', list: ['Riz']}], | ||
sups: [], | ||
}); | ||
|
||
fireEvent.click(secondButton); | ||
expect(mockSetOrderDetails).toHaveBeenCalledWith({ | ||
details: [{name: 'Accompagnement', list: ['Frite']}], | ||
sups: [], | ||
}); | ||
}); | ||
|
||
test('updates an option when mutiple = true', () => { | ||
render( | ||
<FoodDetail | ||
name={name} | ||
data={data} | ||
multiple={true} | ||
orderDetails={orderDetails} | ||
setOrderDetails={mockSetOrderDetails} | ||
/> | ||
); | ||
|
||
const firstButton = screen.getByText("Frite").closest('div'); | ||
fireEvent.click(firstButton.querySelector('button')); | ||
test('handles multiple selection', () => { | ||
const { getByText } = render(<FoodDetail {...defaultProps} />); | ||
|
||
const secondButton = screen.getByText("Semoule").closest('div'); | ||
fireEvent.click(secondButton.querySelector('button')); | ||
const firstButton = getByText('Riz').closest('button'); | ||
const secondButton = getByText('Frite').closest('button'); | ||
|
||
expect(firstButton).toHaveClass('bg-kitchen-food-detail-selected'); | ||
expect(secondButton).toHaveClass('bg-kitchen-food-detail-selected'); | ||
fireEvent.click(firstButton); | ||
expect(mockSetOrderDetails).toHaveBeenCalledWith({ | ||
details: [{name: 'Accompagnement', list: ['Riz']}], | ||
sups: [], | ||
}); | ||
|
||
fireEvent.click(secondButton); | ||
expect(mockSetOrderDetails).toHaveBeenCalledWith({ | ||
details: [{ name: name, list: expect.arrayContaining(["Frite"]) }], | ||
sups: [] | ||
details: [{name: 'Accompagnement', list: ['Frite', 'Riz']}], | ||
sups: [], | ||
}); | ||
|
||
fireEvent.click(firstButton); | ||
expect(mockSetOrderDetails).toHaveBeenCalledWith({ | ||
details: [{ name: name, list: expect.arrayContaining(["Semoule"]) }], | ||
sups: [] | ||
details: [{name: 'Accompagnement', list: ['Frite']}], | ||
sups: [], | ||
}); | ||
}); | ||
|
||
test('deselects an option', () => { | ||
const orderDetailsWithSelection = { | ||
details: [{ name: name, list: ["Frite"] }], | ||
sups: [] | ||
test('removes details', () => { | ||
const initialProps = { | ||
...defaultProps, | ||
orderDetails: { | ||
details: [{name: 'Accompagnement', list: ['Riz']}], | ||
sups: [], | ||
}, | ||
}; | ||
|
||
render( | ||
<FoodDetail | ||
name={name} | ||
data={data} | ||
multiple={false} | ||
orderDetails={orderDetailsWithSelection} | ||
setOrderDetails={mockSetOrderDetails} | ||
/> | ||
); | ||
|
||
const selectedButton = screen.getByText("Frite").closest('div'); | ||
expect(selectedButton).toHaveClass('bg-kitchen-food-detail-selected'); | ||
fireEvent.click(selectedButton.querySelector('button')); | ||
const { getByText } = render(<FoodDetail {...initialProps} />); | ||
|
||
expect(selectedButton).toHaveClass('bg-kitchen-food-detail'); | ||
const firstButton = getByText('Riz').closest('button'); | ||
|
||
fireEvent.click(firstButton); | ||
expect(mockSetOrderDetails).toHaveBeenCalledWith({ | ||
details: [{ name: name, list: [] }], | ||
sups: [] | ||
details: [], | ||
sups: [], | ||
}); | ||
}); | ||
}); | ||
|
||
test('updates color', () => { | ||
const { getByText } = render(<FoodDetail {...defaultProps} />); | ||
|
||
const firstButton = getByText('Riz').closest('div'); | ||
const secondButton = getByText('Frite').closest('div'); | ||
|
||
expect(firstButton).toHaveClass('bg-kitchen-food-detail'); | ||
expect(secondButton).toHaveClass('bg-kitchen-food-detail'); | ||
|
||
fireEvent.click(firstButton.querySelector('button')); | ||
expect(firstButton).toHaveClass('bg-kitchen-food-detail-selected'); | ||
expect(secondButton).toHaveClass('bg-kitchen-food-detail'); | ||
}); | ||
}); |