Co-authored-by: Alexis DRAI <alexis.drai@etu.uca.fr> Reviewed-on: #22main
parent
faf7d8c34b
commit
226667107d
@ -0,0 +1,100 @@
|
|||||||
|
// redux/reducers/__tests__/moveReducer.test.ts
|
||||||
|
import moveReducer from '../moveReducer';
|
||||||
|
import { CREATE_MOVE, DELETE_MOVE, GET_MOVES, MOVE_ERROR, UPDATE_MOVE } from '../../constants';
|
||||||
|
import { Move } from "../../../entities/Move";
|
||||||
|
import { MoveCategoryName } from "../../../entities/MoveCategoryName";
|
||||||
|
import { TypeName } from "../../../entities/TypeName";
|
||||||
|
|
||||||
|
describe('moveReducer', () => {
|
||||||
|
const initialState = {
|
||||||
|
moves: [],
|
||||||
|
error: null
|
||||||
|
};
|
||||||
|
|
||||||
|
it('returns the initial state when an action type is not passed', () => {
|
||||||
|
// @ts-ignore
|
||||||
|
const reducer = moveReducer(undefined, { type: null });
|
||||||
|
|
||||||
|
expect(reducer).toEqual(initialState);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles GET_MOVES action', () => {
|
||||||
|
const moves: Move[] = [{
|
||||||
|
id: '1',
|
||||||
|
name: 'Test Move',
|
||||||
|
category: MoveCategoryName.PHYSICAL,
|
||||||
|
power: 100,
|
||||||
|
accuracy: 100,
|
||||||
|
type: { name: TypeName.NORMAL, weakAgainst: [], effectiveAgainst: [] },
|
||||||
|
schemaVersion: 2
|
||||||
|
}];
|
||||||
|
const reducer = moveReducer(
|
||||||
|
initialState,
|
||||||
|
{ type: GET_MOVES, payload: moves }
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(reducer).toEqual({ ...initialState, moves });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles CREATE_MOVE action', () => {
|
||||||
|
const move: Move = {
|
||||||
|
id: '1',
|
||||||
|
name: 'Test Move',
|
||||||
|
category: MoveCategoryName.PHYSICAL,
|
||||||
|
power: 100,
|
||||||
|
accuracy: 100,
|
||||||
|
type: { name: TypeName.NORMAL, weakAgainst: [], effectiveAgainst: [] },
|
||||||
|
schemaVersion: 2
|
||||||
|
};
|
||||||
|
const reducer = moveReducer(
|
||||||
|
initialState,
|
||||||
|
{ type: CREATE_MOVE, payload: move }
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(reducer).toEqual({ ...initialState, moves: [move] });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles UPDATE_MOVE action', () => {
|
||||||
|
const initialMove: Move = {
|
||||||
|
id: '1',
|
||||||
|
name: 'Test Move',
|
||||||
|
category: MoveCategoryName.PHYSICAL,
|
||||||
|
power: 100,
|
||||||
|
accuracy: 100,
|
||||||
|
type: { name: TypeName.NORMAL, weakAgainst: [], effectiveAgainst: [] },
|
||||||
|
schemaVersion: 2
|
||||||
|
};
|
||||||
|
const updatedMove: Move = { ...initialMove, name: 'Updated Move' };
|
||||||
|
const reducer = moveReducer(
|
||||||
|
{ ...initialState, moves: [initialMove] },
|
||||||
|
{ type: UPDATE_MOVE, payload: updatedMove }
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(reducer).toEqual({ ...initialState, moves: [updatedMove] });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles DELETE_MOVE action', () => {
|
||||||
|
const move: Move = {
|
||||||
|
id: '1',
|
||||||
|
name: 'Test Move',
|
||||||
|
category: MoveCategoryName.PHYSICAL,
|
||||||
|
power: 100,
|
||||||
|
accuracy: 100,
|
||||||
|
type: { name: TypeName.NORMAL, weakAgainst: [], effectiveAgainst: [] },
|
||||||
|
schemaVersion: 2
|
||||||
|
};
|
||||||
|
const reducer = moveReducer(
|
||||||
|
{ ...initialState, moves: [move] },
|
||||||
|
// @ts-ignore
|
||||||
|
{ type: DELETE_MOVE, payload: move.id }
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(reducer).toEqual(initialState);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles MOVE_ERROR action', () => {
|
||||||
|
const reducer = moveReducer(initialState, { type: MOVE_ERROR, payload: 'Error message' });
|
||||||
|
|
||||||
|
expect(reducer).toEqual({ ...initialState, error: 'Error message' });
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,71 @@
|
|||||||
|
// screens/moves/__tests__/MoveDetailScreen.test.tsx
|
||||||
|
|
||||||
|
import React from 'react';
|
||||||
|
import { render, fireEvent } from '@testing-library/react-native';
|
||||||
|
import configureStore from 'redux-mock-store';
|
||||||
|
import { Provider } from 'react-redux';
|
||||||
|
import MoveDetailScreen from '../MoveDetailScreen';
|
||||||
|
import { MOVE_FORM } from "../../../navigation/constants";
|
||||||
|
|
||||||
|
const mockStore = configureStore([]);
|
||||||
|
|
||||||
|
describe('MoveDetailScreen', () => {
|
||||||
|
let store;
|
||||||
|
let component: JSX.Element;
|
||||||
|
// @ts-ignore
|
||||||
|
let navigation;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
const mockMove = {
|
||||||
|
id: '1',
|
||||||
|
name: 'Test Move',
|
||||||
|
category: 'PHYSICAL',
|
||||||
|
power: '100',
|
||||||
|
accuracy: '100',
|
||||||
|
type: {
|
||||||
|
name: 'NORMAL',
|
||||||
|
weakAgainst: [],
|
||||||
|
effectiveAgainst: [],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
store = mockStore({
|
||||||
|
move: {
|
||||||
|
moves: [mockMove],
|
||||||
|
error: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
navigation = { navigate: jest.fn(), setOptions: jest.fn() };
|
||||||
|
const route = { params: { move: mockMove } };
|
||||||
|
|
||||||
|
component = (
|
||||||
|
<Provider store={store}>
|
||||||
|
{/* @ts-ignore */}
|
||||||
|
<MoveDetailScreen navigation={navigation} route={route} />
|
||||||
|
</Provider>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders correctly', () => {
|
||||||
|
const { getByText } = render(component);
|
||||||
|
expect(getByText('Name: Test Move')).toBeTruthy();
|
||||||
|
expect(getByText('Category: PHYSICAL')).toBeTruthy();
|
||||||
|
expect(getByText('Power: 100')).toBeTruthy();
|
||||||
|
expect(getByText('Accuracy: 100')).toBeTruthy();
|
||||||
|
expect(getByText('Type: NORMAL')).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('navigates to the form screen when the edit button is pressed', () => {
|
||||||
|
const { getByText } = render(component);
|
||||||
|
fireEvent.press(getByText('Edit Move'));
|
||||||
|
// @ts-ignore
|
||||||
|
expect(navigation.navigate).toHaveBeenCalledWith(MOVE_FORM, { move: expect.any(Object) });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets the navigation options correctly', () => {
|
||||||
|
render(component);
|
||||||
|
// @ts-ignore
|
||||||
|
expect(navigation.setOptions).toHaveBeenCalledWith({ title: 'Test Move' });
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in new issue