diff --git a/iut-expo-starter/.expo/packager-info.json b/iut-expo-starter/.expo/packager-info.json index 36cff4cb..765d447b 100644 --- a/iut-expo-starter/.expo/packager-info.json +++ b/iut-expo-starter/.expo/packager-info.json @@ -1,6 +1,6 @@ { "expoServerPort": null, - "packagerPort": null, + "packagerPort": 19000, "packagerPid": null, "expoServerNgrokUrl": null, "packagerNgrokUrl": null, diff --git a/iut-expo-starter/.expo/test/redux-store.test.ts b/iut-expo-starter/.expo/test/redux-store.test.ts deleted file mode 100644 index 998368f0..00000000 --- a/iut-expo-starter/.expo/test/redux-store.test.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { getCityList } from 'redux/actions/getFavoriteCity'; - -describe("Actions tests", () => { - it('should create an action with FETCH_FAVORITE_CITY type', () => { - expect(getFavoriteCity().toEqual({type: 'FETCH_FAVORITE_CITY'})); - }) -}) \ No newline at end of file diff --git a/iut-expo-starter/package.json b/iut-expo-starter/package.json index 5380b8f7..49e97dbb 100644 --- a/iut-expo-starter/package.json +++ b/iut-expo-starter/package.json @@ -48,7 +48,7 @@ "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)" ], "testMatch": [ - "**.test.js" + "**.test.ts" ], "testEnvironment": "node", "testEnvironmentOptions": { diff --git a/iut-expo-starter/test/component.test.ts b/iut-expo-starter/test/component.test.ts new file mode 100644 index 00000000..798ea004 --- /dev/null +++ b/iut-expo-starter/test/component.test.ts @@ -0,0 +1,50 @@ +import React from 'react'; +import {expect} from '@jest/globals'; +import NounoursListItem from "../components/NounoursListItem"; +import {Provider} from "react-redux"; +import {configureStore} from "@reduxjs/toolkit"; +import {fireEvent, render, screen} from '@testing-library/react-native' +import '@testing-library/jest-native/extend-expect'; +import testReducer from "./testReducer"; + + +// To wait for things like animation to be fully loaded. +jest.useFakeTimers(); + +// Configure store for testing purpose +const store = configureStore({ + reducer: { + appReducer: testReducer, + }, + middleware: (getDefaultMiddleware) => + getDefaultMiddleware({ + serializableCheck: false + }) +}); + +// When using a Data Provider (like redux) in your App, you will need to wrap all your tested component into a test Provider +// You cannot use the exact same provider and store you create in App.tsx file because here you want mocked data into your store +const Wrapper = ({children}) => ({children}); + +describe('', () => { + test('Assert displayed values', () => { + const expectedNounoursInfos = store.getState().appReducer.nounours[0]; + + render( + + ) + + // Get displayed text component and assert that its value contains our nounours name + expect(screen.getByTestId('nounours-name')).toHaveTextContent(expectedNounoursInfos.name) + expect(screen.getByTestId('nounours-nbPoils')).toHaveTextContent(expectedNounoursInfos.nbPoils) + expect(screen.getByTestId('nounours-age')).toHaveTextContent(expectedNounoursInfos.age) + + expect(screen.getByTestId("nounours-image")).toHaveProp("source", {uri: expectedNounoursInfos.image}) + + let nounoursListSize = store.getState().appReducer.nounours.length; + // Use @testing-library to press our "delete" button + fireEvent.press(screen.getByTestId("remove-button")) + // Make sure that this press action has delete our nounours from our current state (in our test store). + expect(store.getState().appReducer.nounours.length).toBe(nounoursListSize - 1) + }) +}); \ No newline at end of file diff --git a/iut-expo-starter/test/reducers.test.ts b/iut-expo-starter/test/reducers.test.ts new file mode 100644 index 00000000..defca485 --- /dev/null +++ b/iut-expo-starter/test/reducers.test.ts @@ -0,0 +1,22 @@ +import { FAVORITE_CITY_DATA, WEATHER_DATA } from "../data/stub"; +import appReducer from "../redux/reducers/appReducer" + +describe('test reducer', () => { + let initialState = { + weatherListSearched : [], + favoriteWeather : null, + favoriteCity : null, + weatherList : [] + } + it('should return initial state', () => { + expect(appReducer(undefined, {})).toEqual(initialState); + }) + + it('should handle favorite city', () => { + expect(appReducer(initialState, {type : 'FETCH_FAVORITE_CITY', FAVORITE_CITY_DATA})).toEqual({weatherList : [], favoriteCity : FAVORITE_CITY_DATA}) + }) + + it('should handle weather', () => { + expect(appReducer(initialState, {type : 'FETCH_WEATHER', WEATHER_DATA})).toEqual({weatherList : [WEATHER_DATA], favoriteCity : null}) + }) +}) \ No newline at end of file diff --git a/iut-expo-starter/test/redux-store.test.ts b/iut-expo-starter/test/redux-store.test.ts index cabbf3bc..3c6d0960 100644 --- a/iut-expo-starter/test/redux-store.test.ts +++ b/iut-expo-starter/test/redux-store.test.ts @@ -1,5 +1,22 @@ +import { ExclusiveGesture } from 'react-native-gesture-handler/lib/typescript/handlers/gestures/gestureComposition'; +import { City, FAVORITE_CITY_DATA, WEATHER_DATA } from '../data/stub'; +import { getFavoriteCity } from '../redux/actions/getFavoriteCity'; +import { setFavoriteCity } from '../redux/actions/setFavoriteCity'; +import { setWeather } from '../redux/actions/setWeather'; + describe("Actions tests", () => { it('should create an action with FETCH_FAVORITE_CITY type', () => { - expect(getFavoriteCity().toEqual({type: 'FETCH_FAVORITE_CITY'})); + const expectation = { + type : 'FETCH_FAVORITE_CITY', + payload : FAVORITE_CITY_DATA + } + expect(setFavoriteCity(FAVORITE_CITY_DATA)).toEqual(expectation); + }) + it('should create an action with FETCH_WEATHER type', () => { + const expectation = { + type : 'FETCH_WEATHER', + payload : WEATHER_DATA + } + expect(setWeather(WEATHER_DATA)).toEqual(expectation); }) }) \ No newline at end of file diff --git a/iut-expo-starter/test/testReducer.ts b/iut-expo-starter/test/testReducer.ts new file mode 100644 index 00000000..9f5dc64c --- /dev/null +++ b/iut-expo-starter/test/testReducer.ts @@ -0,0 +1,14 @@ +import {WEATHER_DATA} from '../data/stub'; +import appReducer from "../redux/reducers/appReducer"; + +// Mock values used in tests +const initialState = { + weatherList: WEATHER_DATA, //Here you defined a static list loaded in the initial state. In your real store, this list is usually empty. + favoriteCity: [], +} + +// Reducer for tests => Just call the "true" reducer with our mocked data as initial state +// @ts-ignore +export default testReducer = (state = initialState, action) => { + return appReducer(initialState, action); +} \ No newline at end of file