-
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.
- Loading branch information
a.allhyani
committed
Nov 14, 2024
1 parent
1054d6d
commit 7a611e1
Showing
9 changed files
with
152 additions
and
21 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
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,31 +1,29 @@ | ||
// App.test.js | ||
|
||
import React from 'react'; | ||
import { render } from '@testing-library/react'; // Import render method from React Testing Library | ||
import { Provider } from 'react-redux'; // Import Provider to wrap the app | ||
import { configureStore } from '@reduxjs/toolkit'; // Import configureStore | ||
import App from './App'; // Import your main App component | ||
import reducer from '../reducers'; // Import your rootReducer | ||
import '@testing-library/jest-dom/extend-expect'; // Import jest-dom for additional matchers | ||
import { render } from '@testing-library/react'; | ||
import { Provider } from 'react-redux'; | ||
import { configureStore } from '@reduxjs/toolkit'; | ||
import App from './App.js'; | ||
import reducer from '../reducers/index.js'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
|
||
// Create a function to set up the Redux store for testing | ||
const setUpStore = (initialState) => { | ||
return configureStore({ | ||
reducer, | ||
preloadedState: initialState, // Optional: set initial state if needed | ||
preloadedState: initialState, | ||
}); | ||
}; | ||
|
||
describe('App Component', () => { | ||
it('renders without crashing and includes SongList', () => { | ||
const store = setUpStore(); // Create a store instance | ||
const store = setUpStore(); | ||
const { getByText } = render( | ||
<Provider store={store}> {/* Wrap your app with Provider */} | ||
<Provider store={store}> | ||
<App /> | ||
</Provider> | ||
); | ||
|
||
// Check if the App component renders and includes the SongList component | ||
expect(getByText(/song list/i)).toBeInTheDocument(); // Adjust this text to match the actual text in your SongList component | ||
expect(getByText(/song list/i)).toBeInTheDocument(); | ||
}); | ||
}); |
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,8 +1,30 @@ | ||
import React from "react"; | ||
import { connect } from "react-redux"; | ||
|
||
const SongDetail = () => { | ||
return <div>SongDetail</div>; | ||
const SongDetail = ({ song }) => { | ||
// Log the song prop to see its structure | ||
console.log(song); | ||
|
||
// Check if a song is selected | ||
if (!song) { | ||
return <div>Select a song to see the details</div>; | ||
} | ||
|
||
// Render song details | ||
return ( | ||
<div> | ||
<h2>Song Details</h2> | ||
<p><strong>Title:</strong> {song.title}</p> | ||
<p><strong>Duration:</strong> {song.duration}</p> | ||
{/* Add more song details as needed */} | ||
</div> | ||
); | ||
}; | ||
|
||
const mapStateToProps = (state) => { | ||
return { | ||
song: state.selectedSong, // Assuming selectedSong is an object with song details | ||
}; | ||
}; | ||
|
||
export default SongDetail; | ||
export default connect(mapStateToProps)(SongDetail); |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// SongDetail.test.js | ||
|
||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { Provider } from 'react-redux'; | ||
import { createStore } from 'redux'; | ||
import SongDetail from './SongDetail.js'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
|
||
const mockReducer = (state = { selectedSong: null }, action) => { | ||
switch (action.type) { | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const renderWithRedux = (component, { initialState, store = createStore(mockReducer, initialState) } = {}) => { | ||
return { | ||
...render(<Provider store={store}>{component}</Provider>), | ||
store, | ||
}; | ||
}; | ||
|
||
describe('SongDetail Component', () => { | ||
test('renders message when no song is selected', () => { | ||
const { getByText } = renderWithRedux(<SongDetail />); | ||
expect(getByText(/select a song to see the details/i)).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders song details when a song is selected', () => { | ||
const initialState = { | ||
selectedSong: { | ||
title: 'Shape of You', | ||
duration: '3:53', | ||
}, | ||
}; | ||
|
||
const { getByText } = renderWithRedux(<SongDetail />, { initialState }); | ||
|
||
expect(getByText(/song details/i)).toBeInTheDocument(); | ||
expect(getByText(/title:/i)).toHaveTextContent('Shape of You'); | ||
expect(getByText(/duration:/i)).toHaveTextContent('3:53'); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// songsReducer.test.js | ||
|
||
import { combineReducers } from "redux"; | ||
import songsReducer, { selectedSongReducer } from "./index"; // Adjust the import path | ||
|
||
describe("Reducers", () => { | ||
describe("songsReducer", () => { | ||
it("should return the initial state and a list of songs", () => { | ||
const expectedState = [ | ||
{ id: 1, title: "No Scrubs", duration: "4:05" }, | ||
{ id: 2, title: "Macarena", duration: "2:30" }, | ||
{ id: 3, title: "All Star", duration: "3:15" }, | ||
{ id: 4, title: "I Want it that Way", duration: "4:45" }, | ||
{ id: 5, title: "Mohammed Abdo", duration: "5:05" }, | ||
]; | ||
|
||
const state = songsReducer(undefined, {}); | ||
expect(state).toEqual(expectedState ); | ||
}); | ||
}); | ||
|
||
describe("selectedSongReducer", () => { | ||
it("should return the selected song when action type is SONG_SELECTED", () => { | ||
const action = { | ||
type: "SONG_SELECTED", | ||
payload: { id: 1, title: "No Scrubs", duration: "4:05" }, | ||
}; | ||
|
||
const state = selectedSongReducer(null, action); | ||
expect(state).toEqual(action.payload); | ||
}); | ||
|
||
it("should return the previous state when action type is not SONG_SELECTED", () => { | ||
const action = { | ||
type: "OTHER_ACTION", | ||
}; | ||
|
||
const previousState = { id: 1, title: "No Scrubs", duration: "4:05" }; | ||
const state = selectedSongReducer(previousState, action); | ||
expect(state).toEqual(previousState); | ||
}); | ||
}); | ||
|
||
describe("combined reducers", () => { | ||
it("should return the initial state for combined reducers", () => { | ||
const combinedReducer = combineReducers({ | ||
songs: songsReducer, | ||
selectedSong: selectedSongReducer, | ||
}); | ||
|
||
const state = combinedReducer(undefined, {}); | ||
expect(state).toEqual({ | ||
songs: [ | ||
{ id: 1, title: "No Scrubs", duration: "4:05" }, | ||
{ id: 2, title: "Macarena", duration: "2:30" }, | ||
{ id: 3, title: "All Star", duration: "3:15" }, | ||
{ id: 4, title: "I Want it that Way", duration: "4:45" }, | ||
{ id: 5, title: "Mohammed Abdo", duration: "5:05" }, | ||
], | ||
selectedSong: null, | ||
}); | ||
}); | ||
}); | ||
}); |
Empty file.