From 9354634b6de83997bb7645651ab8a70e5b5a6835 Mon Sep 17 00:00:00 2001 From: anperederi Date: Thu, 4 Apr 2024 21:01:18 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Add=20somes=20failings=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/JokeDetailTest.test.js | 6 +- src/components/JokeHomeSquareTest.test.js | 31 ++++++++++ src/components/JokeItemTest.test.js | 31 ++++++++++ src/components/JokeItems.tsx | 9 +-- src/components/JokesHomeSquareTest.test.js | 29 ++++++++++ src/components/JokesItemTest.test.js | 29 ++++++++++ src/model/CustomJokeTest.test.js | 2 +- src/model/JokeFactoryTest.test.js | 44 +++++++++++++++ src/model/JokeTest.test.js | 9 --- src/model/SampleJokeTest.test.js | 47 +++++++++++++++ src/navigation/NavigationBar.tsx | 8 +-- src/navigation/NavigationBarTest.test.js | 22 ++++++++ src/navigation/StackNavigationTest.test.js | 26 +++++++++ src/screens/AddScreen.tsx | 13 ++--- src/screens/AddScreenTest.test.js | 66 ++++++++++++++++++++++ src/screens/Catalogue.tsx | 1 - src/screens/CatalogueTest.test.js | 58 +++++++++++++++++++ src/screens/Favorites.tsx | 1 - src/screens/FavoritesTest.test.js | 57 +++++++++++++++++++ src/screens/HomeScreen.tsx | 4 +- src/screens/HomeScreenTest.test.js | 41 ++++++++++++++ src/screens/JokeDetailScreenTest.test.js | 55 ++++++++++++++++++ src/screens/Settings.tsx | 8 +-- src/screens/SettingsTest.test.js | 53 +++++++++++++++++ 24 files changed, 609 insertions(+), 41 deletions(-) create mode 100644 src/model/JokeFactoryTest.test.js create mode 100644 src/model/SampleJokeTest.test.js create mode 100644 src/navigation/NavigationBarTest.test.js create mode 100644 src/navigation/StackNavigationTest.test.js create mode 100644 src/screens/AddScreenTest.test.js create mode 100644 src/screens/CatalogueTest.test.js create mode 100644 src/screens/FavoritesTest.test.js create mode 100644 src/screens/HomeScreenTest.test.js create mode 100644 src/screens/JokeDetailScreenTest.test.js create mode 100644 src/screens/SettingsTest.test.js diff --git a/src/components/JokeDetailTest.test.js b/src/components/JokeDetailTest.test.js index a8182a9..da84abb 100644 --- a/src/components/JokeDetailTest.test.js +++ b/src/components/JokeDetailTest.test.js @@ -1,7 +1,7 @@ import React from 'react'; import { render, fireEvent } from '@testing-library/react-native'; -import JokeDetail from '../path/to/JokeDetail'; -import {describe} from "@jest/globals"; // Remplacez le chemin par le bon chemin +import JokeDetail from './JokeDetail'; +import {describe, expect, test} from "@jest/globals"; describe('JokeDetail Component', () => { const sampleJoke = { @@ -49,6 +49,6 @@ describe('JokeDetail Component', () => { const deleteButton = getByTestId('delete-button'); fireEvent.press(deleteButton); expect(deleteMock).toHaveBeenCalledWith(customJoke.id); - expect(dispatchMock).toHaveBeenCalledTimes(2); // Assuming one dispatch for delete and one for update list + expect(dispatchMock).toHaveBeenCalledTimes(2); }); }); \ No newline at end of file diff --git a/src/components/JokeHomeSquareTest.test.js b/src/components/JokeHomeSquareTest.test.js index e69de29..8fdecfd 100644 --- a/src/components/JokeHomeSquareTest.test.js +++ b/src/components/JokeHomeSquareTest.test.js @@ -0,0 +1,31 @@ +import React from 'react'; +import { render } from '@testing-library/react-native'; +import JokeHomeSquare from './JokeHomeSquare'; +import { SampleJoke } from '../model/SampleJoke'; +import { CustomJoke } from '../model/CustomJoke'; +import {describe, expect, it} from "@jest/globals"; + +const sampleJoke = new SampleJoke(1, 'Type', 'Setup', 'Image', 'Punchline'); +const customJoke = new CustomJoke('ID', 'Type', 'Setup', 'Image', 'Punchline'); + +describe('JokeHomeSquare Component', () => { + it('renders correctly with SampleJoke', () => { + const { getByText, getByTestId } = render(); + + expect(getByTestId('topBackground')).toBeTruthy(); + expect(getByTestId('bottomBackground')).toBeTruthy(); + expect(getByTestId('jokeImage')).toBeTruthy(); + expect(getByText('Résumé de la blague')).toBeTruthy(); + expect(getByText('Type, Punchline')).toBeTruthy(); + }); + + it('renders correctly with CustomJoke', () => { + const { getByText, getByTestId } = render(); + + expect(getByTestId('topBackground')).toBeTruthy(); + expect(getByTestId('bottomBackground')).toBeTruthy(); + expect(getByTestId('jokeImage')).toBeTruthy(); + expect(getByText('Résumé de la blague')).toBeTruthy(); + expect(getByText('Type, Punchline')).toBeTruthy(); + }); +}); diff --git a/src/components/JokeItemTest.test.js b/src/components/JokeItemTest.test.js index e69de29..ba82a94 100644 --- a/src/components/JokeItemTest.test.js +++ b/src/components/JokeItemTest.test.js @@ -0,0 +1,31 @@ +import React from 'react'; +import { render } from '@testing-library/react-native'; +import JokeItem from './JokeItem'; +import { SampleJoke } from '../model/SampleJoke'; +import { CustomJoke } from '../model/CustomJoke'; +import {describe, expect, it} from "@jest/globals"; + +const sampleJoke = new SampleJoke(1, 'Type', 'Setup', 'Image', 'Punchline'); +const customJoke = new CustomJoke('ID', 'Type', 'Setup', 'Image', 'Punchline'); + +describe('JokeItem Component', () => { + it('renders correctly with SampleJoke', () => { + const { getByText, getByTestId } = render(); + + expect(getByTestId('color')).toBeTruthy(); + expect(getByTestId('jokeImage')).toBeTruthy(); + expect(getByText('Résumé de la blague')).toBeTruthy(); + expect(getByText('Type, Punchline')).toBeTruthy(); + expect(getByText('Type')).toBeTruthy(); + }); + + it('renders correctly with CustomJoke', () => { + const { getByText, getByTestId } = render(); + + expect(getByTestId('color')).toBeTruthy(); + expect(getByTestId('jokeImage')).toBeTruthy(); + expect(getByText('Résumé de la blague')).toBeTruthy(); + expect(getByText('Type, Punchline')).toBeTruthy(); + expect(getByText('Type')).toBeTruthy(); + }); +}); diff --git a/src/components/JokeItems.tsx b/src/components/JokeItems.tsx index 01b2375..b12f12f 100644 --- a/src/components/JokeItems.tsx +++ b/src/components/JokeItems.tsx @@ -18,7 +18,7 @@ export default function JokeItems(props: JokeListItemProps) { data={props.jokes} keyExtractor={(item) => item.id.toString()} renderItem={ - ({ item }: { item: CustomJoke | SampleJoke }) => ( + ({item}: { item: CustomJoke | SampleJoke }) => ( // @ts-ignore navigation.navigate("JokeDetails", {"idJoke": item.id})}> @@ -27,9 +27,4 @@ export default function JokeItems(props: JokeListItemProps) { } /> ); -} - - -const styles = StyleSheet.create({ - -}) \ No newline at end of file +} \ No newline at end of file diff --git a/src/components/JokesHomeSquareTest.test.js b/src/components/JokesHomeSquareTest.test.js index e69de29..480763f 100644 --- a/src/components/JokesHomeSquareTest.test.js +++ b/src/components/JokesHomeSquareTest.test.js @@ -0,0 +1,29 @@ +import React from 'react'; +import { render } from '@testing-library/react-native'; +import JokesHomeSquare from './JokesHomeSquare'; +import { SampleJoke } from '../model/SampleJoke'; +import { CustomJoke } from '../model/CustomJoke'; +import {describe, expect, it} from "@jest/globals"; + +const sampleJokes = [ + new SampleJoke(1, 'Type1', 'Setup1', 'Image1', 'Punchline1'), + new SampleJoke(2, 'Type2', 'Setup2', 'Image2', 'Punchline2'), +]; +const customJokes = [ + new CustomJoke('ID1', 'Type1', 'Setup1', 'Image1', 'Punchline1'), + new CustomJoke('ID2', 'Type2', 'Setup2', 'Image2', 'Punchline2'), +]; + +describe('JokesHomeSquare Component', () => { + it('renders correctly with SampleJokes', () => { + const { getAllByTestId } = render(); + + expect(getAllByTestId('jokeHomeSquare')).toHaveLength(sampleJokes.length); + }); + + it('renders correctly with CustomJokes', () => { + const { getAllByTestId } = render(); + + expect(getAllByTestId('jokeHomeSquare')).toHaveLength(customJokes.length); + }); +}); diff --git a/src/components/JokesItemTest.test.js b/src/components/JokesItemTest.test.js index e69de29..fe584e3 100644 --- a/src/components/JokesItemTest.test.js +++ b/src/components/JokesItemTest.test.js @@ -0,0 +1,29 @@ +import React from 'react'; +import { render } from '@testing-library/react-native'; +import JokeItems from './JokeItems'; +import { SampleJoke } from '../model/SampleJoke'; +import { CustomJoke } from '../model/CustomJoke'; +import {describe, expect, it} from "@jest/globals"; + +const sampleJokes = [ + new SampleJoke(1, 'Type1', 'Setup1', 'Image1', 'Punchline1'), + new SampleJoke(2, 'Type2', 'Setup2', 'Image2', 'Punchline2'), +]; +const customJokes = [ + new CustomJoke('ID1', 'Type1', 'Setup1', 'Image1', 'Punchline1'), + new CustomJoke('ID2', 'Type2', 'Setup2', 'Image2', 'Punchline2'), +]; + +describe('JokeItems Component', () => { + it('renders correctly with SampleJokes', () => { + const { getAllByTestId } = render(); + + expect(getAllByTestId('jokeItem')).toHaveLength(sampleJokes.length); + }); + + it('renders correctly with CustomJokes', () => { + const { getAllByTestId } = render(); + + expect(getAllByTestId('jokeItem')).toHaveLength(customJokes.length); + }); +}); diff --git a/src/model/CustomJokeTest.test.js b/src/model/CustomJokeTest.test.js index a0b5295..827421c 100644 --- a/src/model/CustomJokeTest.test.js +++ b/src/model/CustomJokeTest.test.js @@ -1,5 +1,5 @@ import { CustomJoke } from './CustomJoke'; -import {describe, expect, test} from "@jest/globals"; // Remplacez le chemin par le bon chemin +import {describe, expect, test} from "@jest/globals"; describe('CustomJoke Class', () => { const id = '1'; diff --git a/src/model/JokeFactoryTest.test.js b/src/model/JokeFactoryTest.test.js new file mode 100644 index 0000000..cba677e --- /dev/null +++ b/src/model/JokeFactoryTest.test.js @@ -0,0 +1,44 @@ +import { JokeFactory } from './JokeFactory'; +import { CustomJoke } from './CustomJoke'; +import { SampleJoke } from './SampleJoke'; +import {describe, expect, it} from "@jest/globals"; + +describe('JokeFactory', () => { + describe('createCustomJokes', () => { + it('should create CustomJoke instances from JSON array', () => { + const jsonArray = '[{"id": "1", "type": "Type1", "setup": "Setup1", "punchline": "Punchline1", "image": "Image1"}, {"id": "2", "type": "Type2", "setup": "Setup2", "punchline": "Punchline2", "image": "Image2"}]'; + const customJokes: CustomJoke[] = JokeFactory.createCustomJokes(jsonArray); + + expect(customJokes).toHaveLength(2); + expect(customJokes[0]).toBeInstanceOf(CustomJoke); + expect(customJokes[0].id).toBe('1'); + expect(customJokes[0].type).toBe('Type1'); + }); + + it('should return an empty array if JSON array is empty', () => { + const jsonArray = '[]'; + const customJokes: CustomJoke[] = JokeFactory.createCustomJokes(jsonArray); + + expect(customJokes).toHaveLength(0); + }); + }); + + describe('createSampleJokes', () => { + it('should create SampleJoke instances from JSON array', () => { + const jsonArray = '[{"id": 1, "type": "Type1", "setup": "Setup1", "punchline": "Punchline1", "image": "Image1"}, {"id": 2, "type": "Type2", "setup": "Setup2", "punchline": "Punchline2", "image": "Image2"}]'; + const sampleJokes: SampleJoke[] = JokeFactory.createSampleJokes(jsonArray); + + expect(sampleJokes).toHaveLength(2); + expect(sampleJokes[0]).toBeInstanceOf(SampleJoke); + expect(sampleJokes[0].id).toBe(1); + expect(sampleJokes[0].type).toBe('Type1'); + }); + + it('should return an empty array if JSON array is empty', () => { + const jsonArray = '[]'; + const sampleJokes: SampleJoke[] = JokeFactory.createSampleJokes(jsonArray); + + expect(sampleJokes).toHaveLength(0); + }); + }); +}); diff --git a/src/model/JokeTest.test.js b/src/model/JokeTest.test.js index a48d09f..9bfedfc 100644 --- a/src/model/JokeTest.test.js +++ b/src/model/JokeTest.test.js @@ -1,14 +1,12 @@ const { Joke } = require('./Joke'); const {expect, it, beforeEach, describe} = require("@jest/globals"); -// Mock class extending the abstract Joke class class MockJoke extends Joke { constructor(type, setup, punchline, image) { super(type, setup, punchline, image); } } -// Test the Joke class describe('Joke Class', () => { let joke; @@ -16,7 +14,6 @@ describe('Joke Class', () => { joke = new MockJoke('type', 'setup', 'punchline', 'image'); }); - // Test the constructor it('should create a new Joke object', () => { expect(joke).toBeDefined(); expect(joke.type).toBe('type'); @@ -25,35 +22,29 @@ describe('Joke Class', () => { expect(joke.image).toBe('image'); }); - // Test the summary() method it('should return a summary of the joke', () => { expect(joke.summary()).toBe('punchline'); }); - // Test the description() method it('should return a textual description of the joke', () => { expect(joke.description()).toBe('type, punchline'); }); - // Test setting and getting the type it('should set and get the type correctly', () => { joke.type = 'newType'; expect(joke.type).toBe('newType'); }); - // Test setting and getting the setup it('should set and get the setup correctly', () => { joke.setup = 'newSetup'; expect(joke.setup).toBe('newSetup'); }); - // Test setting and getting the punchline it('should set and get the punchline correctly', () => { joke.punchline = 'newPunchline'; expect(joke.punchline).toBe('newPunchline'); }); - // Test setting and getting the image it('should set and get the image correctly', () => { joke.image = 'newImage'; expect(joke.image).toBe('newImage'); diff --git a/src/model/SampleJokeTest.test.js b/src/model/SampleJokeTest.test.js new file mode 100644 index 0000000..688d285 --- /dev/null +++ b/src/model/SampleJokeTest.test.js @@ -0,0 +1,47 @@ +import { SampleJoke } from './SampleJoke'; +import {describe, expect, it} from "@jest/globals"; + +describe('SampleJoke', () => { + it('should create a SampleJoke instance with provided values', () => { + const id = 1; + const type = 'Type'; + const setup = 'Setup'; + const image = 'Image'; + const punchline = 'Punchline'; + + const sampleJoke = new SampleJoke(id, type, setup, image, punchline); + + expect(sampleJoke).toBeInstanceOf(SampleJoke); + expect(sampleJoke.id).toBe(id); + expect(sampleJoke.type).toBe(type); + expect(sampleJoke.setup).toBe(setup); + expect(sampleJoke.image).toBe(image); + expect(sampleJoke.punchline).toBe(punchline); + }); + + it('should create a SampleJoke instance with default punchline if not provided', () => { + const id = 1; + const type = 'Type'; + const setup = 'Setup'; + const image = 'Image'; + + const sampleJoke = new SampleJoke(id, type, setup, image); + + expect(sampleJoke).toBeInstanceOf(SampleJoke); + expect(sampleJoke.id).toBe(id); + expect(sampleJoke.type).toBe(type); + expect(sampleJoke.setup).toBe(setup); + expect(sampleJoke.image).toBe(image); + expect(sampleJoke.punchline).toBe(''); + }); + + it('should correctly modify the id', () => { + const sampleJoke = new SampleJoke(1, 'Type', 'Setup', 'Image'); + + expect(sampleJoke.id).toBe(1); + + sampleJoke.id = 2; + + expect(sampleJoke.id).toBe(2); + }); +}); diff --git a/src/navigation/NavigationBar.tsx b/src/navigation/NavigationBar.tsx index dde70b0..7f1d391 100644 --- a/src/navigation/NavigationBar.tsx +++ b/src/navigation/NavigationBar.tsx @@ -1,4 +1,4 @@ -import {DarkTheme, DefaultTheme, NavigationContainer, Theme} from '@react-navigation/native'; +import {NavigationContainer, Theme} from '@react-navigation/native'; import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; import {Image, View} from 'react-native'; @@ -7,7 +7,7 @@ import Favorites from "../screens/Favorites"; import Add from "../screens/AddScreen"; import Settings from "../screens/Settings"; -import {darksalmonColor, greyColor, indigoColor} from "../assets/Theme"; +import {darksalmonColor, DarkTheme, greyColor, indigoColor, LightTheme} from "../assets/Theme"; import StackNavigation from "./StackNavigation"; import {useEffect, useState} from "react"; import {getTheme} from "../redux/thunk/ThemeThunk"; @@ -22,7 +22,7 @@ export default function NavigationBar() { const favoriteIcon = require("../assets/favorite_icon.png"); const settingsIcon = require("../assets/settings_icon.png"); - const [themes, setTheme] = useState(DefaultTheme); + const [themes, setTheme] = useState(LightTheme); useEffect(() => { const fetchTheme = async () => { @@ -38,7 +38,7 @@ export default function NavigationBar() { } return ( - + ', () => { + it('renders correctly', () => { + const { getByText, getByA11yLabel } = render(); + + expect(getByText('Accueil')).toBeTruthy(); + expect(getByText('Catalogue')).toBeTruthy(); + expect(getByText("Ajout d'une blague")).toBeTruthy(); + expect(getByText('Favoris')).toBeTruthy(); + expect(getByText('Parametres')).toBeTruthy(); + + expect(getByA11yLabel('Accueil icon')).toBeTruthy(); + expect(getByA11yLabel('Catalogue icon')).toBeTruthy(); + expect(getByA11yLabel('Ajout d\'une blague icon')).toBeTruthy(); + expect(getByA11yLabel('Favoris icon')).toBeTruthy(); + expect(getByA11yLabel('Parametres icon')).toBeTruthy(); + }); +}); diff --git a/src/navigation/StackNavigationTest.test.js b/src/navigation/StackNavigationTest.test.js new file mode 100644 index 0000000..03a8a3d --- /dev/null +++ b/src/navigation/StackNavigationTest.test.js @@ -0,0 +1,26 @@ +import React from 'react'; +import { render } from '@testing-library/react-native'; +import StackNavigation from './StackNavigation'; +import {describe, expect, it} from "@jest/globals"; + +describe('', () => { + it('renders correctly', () => { + const { getByText } = render(); + + expect(getByText('catalogue')).toBeTruthy(); + expect(getByText('JokeDetails')).toBeTruthy(); + + const header = getByText('catalogue').parentNode; + expect(header).toHaveStyle({ + backgroundColor: 'indigo', + }); + + const headerTitle = getByText('catalogue'); + expect(headerTitle).toHaveStyle({ + color: 'darksalmon', + fontSize: 24, + textAlign: 'center', + paddingBottom: 30, + }); + }); +}); diff --git a/src/screens/AddScreen.tsx b/src/screens/AddScreen.tsx index 552b5fd..ba14f2a 100644 --- a/src/screens/AddScreen.tsx +++ b/src/screens/AddScreen.tsx @@ -1,12 +1,9 @@ -import { customJokeStub } from '../data/stub/CustomJokeStub'; -import { sampleJokeStub } from '../data/stub/SampleJokeStub'; -import JokeItems from "../components/JokeItems"; import '../types/extension'; -import {useDispatch, useSelector} from "react-redux"; -import {Image, StyleSheet, Text, TextInput, TouchableOpacity, View} from "react-native"; -import {darksalmonColor, greyColor, indigoColor, purpleColor, whiteColor} from "../assets/Theme"; -import {getCustomJokesList, getSampleJokesList} from "../redux/thunk/GetThunk"; -import React, {useEffect} from "react"; +import {useDispatch} from "react-redux"; +import {StyleSheet, Text, TextInput, TouchableOpacity, View} from "react-native"; +import {indigoColor, whiteColor} from "../assets/Theme"; +import {getCustomJokesList} from "../redux/thunk/GetThunk"; +import React from "react"; import {postCustomJoke} from "../redux/thunk/PostThunk"; import {useTheme} from "@react-navigation/native"; diff --git a/src/screens/AddScreenTest.test.js b/src/screens/AddScreenTest.test.js new file mode 100644 index 0000000..31c0f46 --- /dev/null +++ b/src/screens/AddScreenTest.test.js @@ -0,0 +1,66 @@ +import React from 'react'; +import { render, fireEvent } from '@testing-library/react-native'; +import AddScreen, { themeSettings } from './AddScreen'; +import { Provider } from 'react-redux'; +import store from '../redux/store'; +import {describe, expect, it} from "@jest/globals"; + +describe('', () => { + it('renders correctly', () => { + const { getByText, getByPlaceholderText } = render( + + + + ); + + expect(getByText('Blague')).toBeTruthy(); + expect(getByPlaceholderText('Inserez votre blague')).toBeTruthy(); + expect(getByText('Chute de la blague')).toBeTruthy(); + expect(getByPlaceholderText('Inserez votre blague')).toBeTruthy(); + expect(getByText('Catégorie')).toBeTruthy(); + expect(getByPlaceholderText('Inserez votre blague')).toBeTruthy(); + expect(getByText('CRÉER')).toBeTruthy(); + expect(getByText('EFFACER')).toBeTruthy(); + }); + + it('handles text input correctly', () => { + const { getByPlaceholderText } = render( + + + + ); + + const jokeInput = getByPlaceholderText('Inserez votre blague'); + const downgradeInput = getByPlaceholderText('Inserez votre blague'); + const categoryInput = getByPlaceholderText('Inserez votre blague'); + + fireEvent.changeText(jokeInput, 'This is a joke'); + fireEvent.changeText(downgradeInput, 'This is a downgrade'); + fireEvent.changeText(categoryInput, 'Funny'); + + expect(jokeInput.props.value).toBe('This is a joke'); + expect(downgradeInput.props.value).toBe('This is a downgrade'); + expect(categoryInput.props.value).toBe('Funny'); + }); + +}); + +describe('themeSettings()', () => { + it('returns correct styles for light theme', () => { + const theme = { + colors: { + background: 'white', + card: 'lightgrey', + text: 'black', + primary: 'blue', + }, + }; + + const styles = themeSettings(theme); + expect(styles.container.backgroundColor).toBe('white'); + expect(styles.textInput.backgroundColor).toBe('lightgrey'); + expect(styles.text.color).toBe('black'); + expect(styles.eraseButton.backgroundColor).toBe('blue'); + expect(styles.createButton.backgroundColor).toBe('blue'); + }); +}); diff --git a/src/screens/Catalogue.tsx b/src/screens/Catalogue.tsx index faa3b1e..eafe900 100644 --- a/src/screens/Catalogue.tsx +++ b/src/screens/Catalogue.tsx @@ -1,7 +1,6 @@ import JokeItems from "../components/JokeItems"; import '../types/extension'; import {Image, SafeAreaView, StyleSheet, Text, TouchableOpacity, View} from "react-native"; -import {darksalmonColor, purpleColor, whiteColor} from "../assets/Theme"; import {useDispatch, useSelector} from "react-redux"; import React, {useEffect, useState} from "react"; import {getCustomJokesList, getSampleJokesList} from "../redux/thunk/GetThunk"; diff --git a/src/screens/CatalogueTest.test.js b/src/screens/CatalogueTest.test.js new file mode 100644 index 0000000..34085b6 --- /dev/null +++ b/src/screens/CatalogueTest.test.js @@ -0,0 +1,58 @@ +import React from 'react'; +import { render, fireEvent } from '@testing-library/react-native'; +import Catalogue, { themeSettings } from './Catalogue'; +import { Provider } from 'react-redux'; +import store from '../redux/store'; +import {describe, expect, it} from "@jest/globals"; + +describe('', () => { + it('renders correctly', () => { + const { getByText, getByTestId } = render( + + + + ); + + expect(getByText('Afficher les exemples')).toBeTruthy(); + expect(getByTestId('toggle-button')).toBeTruthy(); + }); + + it('toggles jokes display correctly', () => { + const { getByTestId } = render( + + + + ); + + const toggleButton = getByTestId('toggle-button'); + + fireEvent.press(toggleButton); + + expect(toggleButton.props.children.props.source).toEqual(hideEye); + + fireEvent.press(toggleButton); + + expect(toggleButton.props.children.props.source).toEqual(eye); + }); + +}); + +describe('themeSettings()', () => { + it('returns correct styles for light theme', () => { + const theme = { + colors: { + background: 'white', + primary: 'blue', + text: 'black', + notification: 'grey', + }, + }; + + const styles = themeSettings(theme); + expect(styles.container.backgroundColor).toBe('white'); + expect(styles.Button.backgroundColor).toBe('blue'); + expect(styles.TextButton.color).toBe('black'); + expect(styles.imageButton.tintColor).toBe('grey'); + }); + +}); diff --git a/src/screens/Favorites.tsx b/src/screens/Favorites.tsx index af4e3e3..f002603 100644 --- a/src/screens/Favorites.tsx +++ b/src/screens/Favorites.tsx @@ -1,7 +1,6 @@ import JokeItems from "../components/JokeItems"; import '../types/extension'; import {Image, SafeAreaView, StyleSheet, Text, TouchableOpacity, View} from "react-native"; -import {darksalmonColor, purpleColor, whiteColor} from "../assets/Theme"; import {useDispatch, useSelector} from "react-redux"; import React, {useEffect, useState} from "react"; import {getCustomJokesList, getSampleJokesList} from "../redux/thunk/GetThunk"; diff --git a/src/screens/FavoritesTest.test.js b/src/screens/FavoritesTest.test.js new file mode 100644 index 0000000..23e4ddd --- /dev/null +++ b/src/screens/FavoritesTest.test.js @@ -0,0 +1,57 @@ +import React from 'react'; +import { render, fireEvent } from '@testing-library/react-native'; +import Favorites, { themeSettings } from './Favorites'; +import { Provider } from 'react-redux'; +import store from '../redux/store'; +import {describe, expect, it} from "@jest/globals"; // Assuming you have a configured Redux store + +describe('', () => { + it('renders correctly', () => { + const { getByText, getByTestId } = render( + + + + ); + + expect(getByText('Afficher les exemples')).toBeTruthy(); + expect(getByTestId('toggle-button')).toBeTruthy(); + }); + + it('toggles jokes display correctly', () => { + const { getByTestId } = render( + + + + ); + + const toggleButton = getByTestId('toggle-button'); + + fireEvent.press(toggleButton); + + expect(toggleButton.props.children.props.source).toEqual(hideEye); + + fireEvent.press(toggleButton); + + expect(toggleButton.props.children.props.source).toEqual(eye); + }); +}); + +describe('themeSettings()', () => { + it('returns correct styles for light theme', () => { + const theme = { + colors: { + background: 'white', + primary: 'blue', + text: 'black', + notification: 'grey', + }, + }; + + const styles = themeSettings(theme); + expect(styles.container.backgroundColor).toBe('white'); + expect(styles.Button.backgroundColor).toBe('white'); + expect(styles.TextButton.color).toBe('grey'); + expect(styles.imageButton.tintColor).toBe('grey'); + }); + +}); diff --git a/src/screens/HomeScreen.tsx b/src/screens/HomeScreen.tsx index b82e840..047f946 100644 --- a/src/screens/HomeScreen.tsx +++ b/src/screens/HomeScreen.tsx @@ -1,15 +1,13 @@ import {Image, StyleSheet, Text, View} from 'react-native'; import '../types/extension'; -import {darksalmonColor, purpleColor, whiteColor} from "../assets/Theme"; import JokesHomeSquare from "../components/JokesHomeSquare"; import Categs from "../components/Categs"; import {useDispatch, useSelector} from "react-redux"; import {useEffect} from "react"; import {getCategoriesList, getLastSampleJokesList} from "../redux/thunk/GetThunk"; import {useTheme} from "@react-navigation/native"; -import styleToBarStyle from "expo-status-bar/build/styleToBarStyle"; -export default function Catalogue() { +export default function HomeScreen() { // @ts-ignore const allJokes = useSelector(state => state.sampleReducer.jokes); // @ts-ignore diff --git a/src/screens/HomeScreenTest.test.js b/src/screens/HomeScreenTest.test.js new file mode 100644 index 0000000..15ca44e --- /dev/null +++ b/src/screens/HomeScreenTest.test.js @@ -0,0 +1,41 @@ +import React from 'react'; +import { render } from '@testing-library/react-native'; +import HomeScreen, { themeSettings } from './HomeScreen'; +import { Provider } from 'react-redux'; +import store from '../redux/store'; +import {describe, expect, it} from "@jest/globals"; + +describe('', () => { + it('renders correctly', () => { + const { getByText, getByTestId } = render( + + + + ); + + expect(getByText('Chat C\'est Drole')).toBeTruthy(); + expect(getByText('Dernières blagues')).toBeTruthy(); + expect(getByText('Top Categories')).toBeTruthy(); + expect(getByTestId('jokes-home-square')).toBeTruthy(); + expect(getByTestId('categs')).toBeTruthy(); + }); + +}); + +describe('themeSettings()', () => { + it('returns correct styles for light theme', () => { + const theme = { + colors: { + background: 'white', + text: 'black', + border: 'grey', + }, + }; + + const styles = themeSettings(theme); + expect(styles.container.backgroundColor).toBe('white'); + expect(styles.textAccueil.color).toBe('black'); + expect(styles.textLastJokes.color).toBe('grey'); + }); + +}); diff --git a/src/screens/JokeDetailScreenTest.test.js b/src/screens/JokeDetailScreenTest.test.js new file mode 100644 index 0000000..a8e5ab3 --- /dev/null +++ b/src/screens/JokeDetailScreenTest.test.js @@ -0,0 +1,55 @@ +import React from 'react'; +import { render } from '@testing-library/react-native'; +import JokeDetailsScreen, { themeSettings } from './JokeDetailsScreen'; +import { Provider } from 'react-redux'; +import store from '../redux/store'; +import {describe, expect, it} from "@jest/globals"; + +describe('', () => { + it('renders correctly for sample joke', () => { + const mockRoute = { + params: { + idJoke: 1, + }, + }; + + const { getByTestId } = render( + + + + ); + + expect(getByTestId('joke-detail')).toBeTruthy(); + }); + + it('renders correctly for custom joke', () => { + const mockRoute = { + params: { + idJoke: 1, + }, + }; + + const { getByTestId } = render( + + + + ); + + expect(getByTestId('joke-detail')).toBeTruthy(); + }); + +}); + +describe('themeSettings()', () => { + it('returns correct styles', () => { + const theme = { + colors: { + background: 'white', + }, + }; + + const styles = themeSettings(theme); + expect(styles.container.backgroundColor).toBe('white'); + }); + +}); diff --git a/src/screens/Settings.tsx b/src/screens/Settings.tsx index a3424bc..77ac08d 100644 --- a/src/screens/Settings.tsx +++ b/src/screens/Settings.tsx @@ -1,14 +1,14 @@ import '../types/extension'; import {Image, StyleSheet, Switch, Text, View} from "react-native"; import { - darksalmonColor, DarkTheme, + darksalmonColor, DarkTheme, LightTheme, whiteColor } from "../assets/Theme"; import React from "react"; -import {DefaultTheme, useTheme} from "@react-navigation/native"; +import {useTheme} from "@react-navigation/native"; import {storeTheme} from "../redux/thunk/ThemeThunk"; -export default function Catalogue() { +export default function Settings() { const light_mode = require("../assets/light_mode.png") const dark_mode = require("../assets/dark_mode.png") const [isDark, setDark] = React.useState(false) @@ -16,7 +16,7 @@ export default function Catalogue() { const toggleTheme = () => { setDark(previousState => { const theme = !previousState; - const newTheme = theme ? DarkTheme : DefaultTheme; + const newTheme = theme ? DarkTheme : LightTheme; storeTheme(newTheme); return theme; }); diff --git a/src/screens/SettingsTest.test.js b/src/screens/SettingsTest.test.js new file mode 100644 index 0000000..e5679a5 --- /dev/null +++ b/src/screens/SettingsTest.test.js @@ -0,0 +1,53 @@ +import React from 'react'; +import { render, fireEvent } from '@testing-library/react-native'; +import Settings, { themeSettings } from './Settings'; +import { Provider } from 'react-redux'; +import store from '../redux/store'; +import {describe, expect, it} from "@jest/globals"; + +describe('', () => { + it('renders correctly', () => { + const { getByTestId } = render( + + + + ); + + expect(getByTestId('settings-title')).toBeTruthy(); + expect(getByTestId('settings-switch')).toBeTruthy(); + }); + + it('switches between light and dark mode', () => { + const { getByTestId } = render( + + + + ); + + const switchElement = getByTestId('settings-switch'); + + expect(switchElement.props.value).toBeFalsy(); + + fireEvent(switchElement, 'onValueChange', true); + + expect(switchElement.props.value).toBeTruthy(); + }); +}); + +describe('themeSettings()', () => { + it('returns correct styles', () => { + const theme = { + colors: { + background: 'white', + text: 'black', + primary: 'blue', + card: 'gray', + }, + }; + + const styles = themeSettings(theme); + expect(styles.container.backgroundColor).toBe('white'); + expect(styles.title.color).toBe('black'); + }); + +});