Skip to content
Snippets Groups Projects
Commit aa19266e authored by Gabriel Clark's avatar Gabriel Clark
Browse files

Merge branch 'main' of git.unl.edu:jackmnolley/react-redux-starter-code

parents 27483535 b9866cee
No related branches found
No related tags found
No related merge requests found
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`the app has one labeled button that displays the value from the store 1`] = `
exports[`the app runs 1`] = `
<div>
<div>
<h1>
......
import { Route } from 'react-router-dom';
import { EndingNumberOutput } from './features/ending-number-output/endingNumberOutput.js';
import { NumberInputField } from './features/number-input-field/numberInputField.js';
import { UnitSelector } from './features/unit-selector/unitSelector.js';
......@@ -5,6 +6,8 @@ import { UnitSelector } from './features/unit-selector/unitSelector.js';
export function App() {
return (
<>
<Route exact path={'/'}>
</Route>
<UnitSelector id="inputUnitSelector"/>
<NumberInputField />
<h1>Ouput Section</h1>
......
/* eslint-disable no-magic-numbers */
import { render } from '@testing-library/react';
import './testing/mockRedux.js';
import { MemoryRouter as Router } from 'react-router-dom';
import { App } from './app.js';
import {
selectValue,
} from './features/counter/counterSlice.js';
jest.mock('./features/counter/counterSlice.js', () => ({
selectValue: jest.fn().mockName('selectValue'),
}));
describe('the app', () => {
test('has one labeled button that displays the value from the store', () => {
selectValue.mockReturnValue(9999);
test('runs', () => {
const { container } = render(
<Router initialEntries={['/']}>
<App />
......
import { useSelector, useDispatch } from 'react-redux';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import styles from './counter.module.css';
import {
selectValue,
setValue,
} from './counterSlice.js';
export function Counter(props) {
const value = useSelector(selectValue);
const dispatch = useDispatch();
const classes = classNames({
[styles.red]: value % 2 === 0,
[styles.blue]: value % 2 === 1,
});
const onClick = () => dispatch(setValue({
value: value + 1,
}));
return (
<label>
{props.label}:&nbsp;
<button className={classes} onClick={onClick}>
{value}
</button>
</label>
);
}
Counter.propTypes = {
label: PropTypes.string.isRequired,
};
.red {
color: rgba(255 0 0 / 100%);
}
.blue {
color: rgba(0 0 255 / 100%);
}
/* eslint-disable no-magic-numbers */
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '../../testing/mockRedux.js';
import { Counter } from './counter.js';
import {
selectValue,
setValue,
} from './counterSlice.js';
jest.mock('./counterSlice.js', () => ({
selectValue: jest.fn().mockName('selectValue'),
setValue: jest.fn().mockName('setValue'),
}));
describe('the button', () => {
test('displays the value from the store', () => {
selectValue.mockReturnValue(9999);
render(<Counter label={'Foo'} />);
expect(screen.getByRole('button')).toHaveTextContent(/^9999$/);
});
test('is red when the value from the store is even', () => {
selectValue.mockReturnValue(9998);
render(<Counter label={'Foo'} />);
expect(screen.getByRole('button')).toHaveClass('red');
});
test('is blue when the value from the store is odd', () => {
selectValue.mockReturnValue(9999);
render(<Counter label={'Foo'} />);
expect(screen.getByRole('button')).toHaveClass('blue');
});
test('increments the value in the store when clicked', () => {
selectValue.mockReturnValue(9999);
render(<Counter label={'Foo'} />);
userEvent.click(screen.getByRole('button'));
expect(setValue).toHaveBeenCalledTimes(1);
expect(setValue).toHaveBeenCalledWith({
value: 9999 + 1,
});
});
});
import { createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter',
initialState: {
value: 0,
},
reducers: {
setValue: (counter, action) => {
const {
value,
} = action.payload;
counter.value = value;
},
},
});
export default counterSlice;
export const {
setValue,
} = counterSlice.actions;
export function selectValue(state) {
return state.counter.value;
}
/* eslint-disable no-magic-numbers */
import counterSlice, {
selectValue,
setValue,
} from './counterSlice.js';
describe('the initial state', () => {
test('has a value of zero', () => {
const state = counterSlice.reducer(undefined, {});
expect(state).toEqual({
value: 0,
});
});
});
describe('selectValue', () => {
test('reads the stored value', () => {
const state = {
value: 9999,
};
const result = selectValue({ [counterSlice.name]: state });
expect(result).toBe(9999);
});
});
describe('setValue', () => {
test('overwrites the stored value', () => {
const state = counterSlice.reducer({
value: 8888,
}, setValue({
value: 9999,
}));
expect(state).toEqual({
value: 9999,
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment