Skip to content

Commit

Permalink
Add coverage for new features
Browse files Browse the repository at this point in the history
  • Loading branch information
tmoitie committed Sep 11, 2023
1 parent 8f5d486 commit ac1bcef
Show file tree
Hide file tree
Showing 5 changed files with 10,003 additions and 14,080 deletions.
4 changes: 4 additions & 0 deletions __mocks__/firebase/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const signInWithEmailAndPassword = jest.fn(async () => {});

export const createUserWithEmailAndPassword = jest.fn(async () => {});

export const signInWithPopup = jest.fn(async () => {});

const authStateListeners = [];

export function onAuthStateChanged(auth, listener) {
Expand All @@ -17,3 +19,5 @@ export async function testDispatchOnAuthStateChanged(user) {
}

export const sendPasswordResetEmail = jest.fn(async () => {});

export const GoogleAuthProvider = jest.requireActual('@firebase/auth').GoogleAuthProvider;
28 changes: 28 additions & 0 deletions src/actions/__tests__/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
getAuth,
sendPasswordResetEmail,
signInWithEmailAndPassword,
signInWithPopup,
signOut as fbSignOut,
GoogleAuthProvider,
} from 'firebase/auth';

import '../settings';
Expand All @@ -17,6 +19,7 @@ import {
forgottenPassword,
LOADING_AUTH, LOADING_RESET, RESET_SENT, SIGNED_IN, SIGNED_OUT,
signIn, signOut, startListener,
signInWithGoogle,
} from '../auth';

// eslint-disable-next-line jest/no-mocks-import
Expand Down Expand Up @@ -81,6 +84,31 @@ describe('authActions', () => {
});
});

describe('signInWithGoogle', () => {
const signInWithGoogleThunk = signInWithGoogle();

test('success', async () => {
const dispatch = jest.fn(async () => {});

const action = await signInWithGoogleThunk(dispatch, getState);

expect(dispatch).toHaveBeenCalledWith({ type: LOADING_AUTH });
expect(signInWithPopup).toHaveBeenCalledWith(firebaseAuth, expect.any(GoogleAuthProvider));
expect(action).toEqual({});
});

test('error', async () => {
const dispatch = jest.fn(async () => {});
signInWithPopup.mockRejectedValueOnce({ message: 'Sign in Error' });

const action = await signInWithGoogleThunk(dispatch, getState);

expect(dispatch).toHaveBeenCalledWith({ type: LOADING_AUTH });
expect(dispatch).toHaveBeenCalledWith({ type: ERROR_AUTH, error: { message: 'Sign in Error' } });
expect(action.type).toEqual(ERROR_AUTH);
});
});

describe('createAccount', () => {
const createAccountThunk = createAccount('[email protected]', '12345678');
test('success', async () => {
Expand Down
16 changes: 16 additions & 0 deletions src/reducers/__tests__/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ describe('reducers/app', () => {
expect(state.currentModal).toBe(null);
});

test('set up default before seasonStart', () => {
MockDate.set('2022-09-01T13:30:00.000Z');
const state = appReducer(undefined, {});
expect(state.date.isSame(moment('2022-09-06T00:00:00.000Z'))).toBe(true);
expect(state.daysSinceSeasonStart).toBe(0);
expect(state.week).toBe(1);
});

test('set up default after seasonEnd', () => {
MockDate.set('2022-12-15T13:30:00.000Z');
const state = appReducer(undefined, {});
expect(state.date.isSame(moment('2022-12-05T00:00:00.000Z'))).toBe(true);
expect(state.daysSinceSeasonStart).toBe(90);
expect(state.week).toBe(13);
});

test('APP/UPDATE_DAYS', () => {
const state = appReducer(undefined, updateDays(10));
expect(state.date.isSame(moment('2022-09-16T00:00:00.000Z'))).toBe(true);
Expand Down
13 changes: 11 additions & 2 deletions src/reducers/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import moment, { duration } from 'moment';
import { CHANGE_MODAL, UPDATE_DAYS } from '../actions/app';
import { SIGNED_IN } from '../actions/auth';
import { seasonStart, weekSeasonStart } from '../config';
import { seasonStart, weekSeasonStart, seasonEnd } from '../config';

const getWeek = (date) => Math.ceil(duration(moment(date).add({ second: 1 }).diff(weekSeasonStart)).asWeeks());

Expand All @@ -27,7 +27,16 @@ export default function app(
let state = initState;

if (state === undefined) {
const currentDate = moment(new Date()).utc().startOf('day');
let currentDate = moment(new Date()).utc().startOf('day');

if (currentDate.isBefore(seasonStart)) {
currentDate = moment(seasonStart).utc().startOf('day');
}

if (currentDate.isAfter(seasonEnd)) {
currentDate = moment(seasonEnd).utc().startOf('day').subtract({ days: 1 });
}

const currentDays = currentDate.diff(seasonStart, 'days');

state = {
Expand Down
Loading

0 comments on commit ac1bcef

Please sign in to comment.