Skip to content

Commit

Permalink
Add/init unit tests and jsdocs for backend and utility functions (#426)
Browse files Browse the repository at this point in the history
* test: add configs and mocks for `firebase`

* docs(`utilities.ts`): add jsdocs to function definitions

* docs(`utilityFunctions.ts`): add jsdocs to function definitions

* docs(`dbOperations.ts`): add jsdocs to function definitions

* docs(`utilities.ts`): add jsdoc to function definition

* docs(`utilities.ts`): add jsdocs to function definitions

* test: add unit tests and setups for utilities functions

* test('utilities.ts`): init unit tests

* fix: fix linting issues in CI/CD

* ci: change configuration for Jest to pull request

* fix: invalid field `Name` in config file

* fix: minor typo (transposition error)

* chore(`jest.config.js`): add comment

* chore(`utilities.test.ts`): reorganize tests into arrange-act-assert

* chore(`utilities.test.ts`): reorganize unit tests structure/hierarchy

---------

Co-authored-by: awpala <[email protected]>
  • Loading branch information
awpala and awpala authored Aug 8, 2023
1 parent 84fa8f7 commit 361c1e9
Show file tree
Hide file tree
Showing 17 changed files with 1,137 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/jest.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Name: 'Jest'
name: 'Jest'
on:
push:
branches:
Expand Down
75 changes: 75 additions & 0 deletions __tests__/utilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { DESC } from '@globals/constants';
import { TCourseId } from '@globals/types';
import {
// mapRatingToColor,
// mapRatingToColorInverted,
mapPayloadToArray,
} from '@src/utilities';

// types
type TInputPayload = {
// eslint-disable-next-line no-unused-vars
[T in TCourseId]: any;
};

describe('frontend utilities tests', () => {
describe('mapRatingToColor()', () => {
it('maps ratings to colors', () => {
// TODO
});
});

describe('mapRatingToColorInverted()', () => {
it('maps ratings to inverted colors', () => {
// TODO
});
});

describe('mapPayloadToArray()', () => {
let inputPayload: Partial<TInputPayload> = {};

beforeEach(() => {
inputPayload = {
'CS-6035': { sortKeyField: 3, courseId: 'CS-6035' },
'CS-6150': { sortKeyField: 1, courseId: 'CS-6150' },
'CS-6200': { sortKeyField: 2, courseId: 'CS-6200' },
};
});

it('returns empty array for empty payload', () => {
const inputPayload = {};
const mappedArray = mapPayloadToArray(inputPayload);
expect(mappedArray).toMatchObject([]);
});

it('maps payload to array, ascending by sortKey', () => {
const mappedArray = mapPayloadToArray(inputPayload, 'sortKeyField');
const expectedOutput = [
{ sortKeyField: 1, courseId: 'CS-6150' },
{ sortKeyField: 2, courseId: 'CS-6200' },
{ sortKeyField: 3, courseId: 'CS-6035' },
];
expect(mappedArray).toMatchObject(expectedOutput);
});

it('maps payload to array, descending by sortKey', () => {
const mappedArray = mapPayloadToArray(inputPayload, 'courseID', DESC);
const expectedOutput = [
{ sortKeyField: 2, courseId: 'CS-6200' },
{ sortKeyField: 1, courseId: 'CS-6150' },
{ sortKeyField: 3, courseId: 'CS-6035' },
];
expect(mappedArray).toMatchObject(expectedOutput);
});

it('falls through in original order if no sortKey specified', () => {
const mappedArray = mapPayloadToArray(inputPayload);
const expectedOutput = [
{ sortKeyField: 3, courseId: 'CS-6035' },
{ sortKeyField: 1, courseId: 'CS-6150' },
{ sortKeyField: 2, courseId: 'CS-6200' },
];
expect(mappedArray).toMatchObject(expectedOutput);
});
});
});
3 changes: 1 addition & 2 deletions firebase/FirebaseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export const storage = getStorage(firebaseApp);
// be running via `yarn fb:emu` first before starting the app via `yarn dev`)
const isEmulatorMode =
process.env.NEXT_PUBLIC_IS_EMULATOR_MODE?.toLowerCase() === 'true';
const isEmulatorEnvironment =
process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test';
const isEmulatorEnvironment = process.env.NODE_ENV === 'development';
if (isEmulatorMode && isEmulatorEnvironment) {
// prevent multiple Firestore emulator connections on re-render -- reference: https://stackoverflow.com/a/74727587
const host =
Expand Down
3 changes: 3 additions & 0 deletions firebase/__mocks__/fbApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
jest.mock('firebase/app', () => ({
initializeApp: jest.fn(() => ({})),
}));
4 changes: 4 additions & 0 deletions firebase/__mocks__/fbAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jest.mock('firebase/auth', () => ({
connectAuthEmulator: jest.fn(),
getAuth: jest.fn(),
}));
12 changes: 12 additions & 0 deletions firebase/__mocks__/fbFirestore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
jest.mock('firebase/firestore', () => ({
getFirestore: jest.fn(() => ({
toJSON: jest.fn(() => ({
settings: { host: '' },
})),
})),
connectFirestoreEmulator: jest.fn(),
doc: jest.fn(),
getDoc: jest.fn(),
setDoc: jest.fn(),
deleteDoc: jest.fn(),
}));
4 changes: 4 additions & 0 deletions firebase/__mocks__/fbStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jest.mock('firebase/storage', () => ({
connectStorageEmulator: jest.fn(),
getStorage: jest.fn(),
}));
75 changes: 75 additions & 0 deletions firebase/__tests__/dbOperations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// mock imports (cf. `/firebase/__mocks__`)
import 'firebase/app';
import 'firebase/firestore';
import 'firebase/auth';
import 'firebase/storage';

describe('backend dbOperations tests', () => {
describe('courses CRUD operations', () => {
describe('getCourses', () => {
it('gets all courses from Firebase Firestore DB', () => {
// TODO
});
});

describe('getCourse()', () => {
// TODO
});

describe('addCourse()', () => {
// TODO
});

describe('updateCourse()', () => {
// TODO
});

describe('deleteCourse()', () => {
// TODO
});
});

describe('reviews CRUD operations', () => {
describe('getReviews()', () => {
// TODO
});

describe('getReviewsRecent()', () => {
// TODO
});

describe('getReview()', () => {
// TODO
});

describe('addReview()', () => {
// TODO
});

describe('updateReview()', () => {
// TODO
});

describe('deleteReview()', () => {
// TODO
});
});

describe('users CRUD operations', () => {
describe('getUser()', () => {
// TODO
});

describe('addUser()', () => {
// TODO
});

describe('editUser()', () => {
// TODO
});

describe('deleteUser()', () => {
// TODO
});
});
});
Loading

1 comment on commit 361c1e9

@vercel
Copy link

@vercel vercel bot commented on 361c1e9 Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

website – ./

website-git-main-omshub.vercel.app
www.omshub.org
website-omshub.vercel.app
omshub.org

Please sign in to comment.