Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sweep Rules] Create unit test for new business logic in regionController.js and Hierarchy.js #188

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions backend/test/Hierarchy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// backend/test/Hierarchy.test.js

// Import necessary modules and functions
const { getSiblings } = require('../models/Hierarchy');

Check failure on line 4 in backend/test/Hierarchy.test.js

View workflow job for this annotation

GitHub Actions / lint-backend

Unable to resolve path to module '../models/Hierarchy'

Check failure on line 4 in backend/test/Hierarchy.test.js

View workflow job for this annotation

GitHub Actions / lint-backend

Missing file extension for "../models/Hierarchy"

// Test cases for getSiblings function
describe('getSiblings', () => {

Check failure on line 7 in backend/test/Hierarchy.test.js

View workflow job for this annotation

GitHub Actions / lint-backend

'describe' is not defined
// Test case for when there are siblings
test('should return an array of sibling hierarchies when there are siblings', () => {

Check failure on line 9 in backend/test/Hierarchy.test.js

View workflow job for this annotation

GitHub Actions / lint-backend

'test' is not defined
// Create a mock hierarchy
const hierarchy = { id: 1, name: 'Hierarchy A' };

// Mock the Hierarchy.findOne method to return the mock hierarchy
Hierarchy.findOne = jest.fn().mockResolvedValue(hierarchy);

Check failure on line 14 in backend/test/Hierarchy.test.js

View workflow job for this annotation

GitHub Actions / lint-backend

'Hierarchy' is not defined

Check failure on line 14 in backend/test/Hierarchy.test.js

View workflow job for this annotation

GitHub Actions / lint-backend

'jest' is not defined

// Call the getSiblings function
const siblings = getSiblings(1);

// Assert that the result is an array of sibling hierarchies
expect(Array.isArray(siblings)).toBe(true);

Check failure on line 20 in backend/test/Hierarchy.test.js

View workflow job for this annotation

GitHub Actions / lint-backend

'expect' is not defined
expect(siblings).toHaveLength(2);

Check failure on line 21 in backend/test/Hierarchy.test.js

View workflow job for this annotation

GitHub Actions / lint-backend

'expect' is not defined
expect(siblings).toContainEqual({ id: 2, name: 'Hierarchy B' });

Check failure on line 22 in backend/test/Hierarchy.test.js

View workflow job for this annotation

GitHub Actions / lint-backend

'expect' is not defined
expect(siblings).toContainEqual({ id: 3, name: 'Hierarchy C' });

Check failure on line 23 in backend/test/Hierarchy.test.js

View workflow job for this annotation

GitHub Actions / lint-backend

'expect' is not defined
});

// Test case for when there are no siblings
test('should return an empty array when there are no siblings', () => {
// Mock the Hierarchy.findOne method to return null
Hierarchy.findOne = jest.fn().mockResolvedValue(null);

// Call the getSiblings function
const siblings = getSiblings(4);

// Assert that the result is an empty array
expect(Array.isArray(siblings)).toBe(true);
expect(siblings).toHaveLength(0);
});
});
11 changes: 11 additions & 0 deletions backend/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "backend-test",
"version": "1.0.0",
"description": "Unit tests for the backend",
"scripts": {
"test": "jest regionController.test.js Hierarchy.test.js"
},
"devDependencies": {
"jest": "^27.0.6"
}
}
39 changes: 39 additions & 0 deletions backend/test/regionController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// backend/test/regionController.test.js

// Import necessary modules and functions
const { getSiblings } = require('../controllers/regionController');
const { Region } = require('../models/Region');

// Test cases for getSiblings function
describe('getSiblings', () => {
// Test case for when the region exists
test('should return an array of sibling regions when the region exists', () => {
// Create a mock region
const region = new Region({ name: 'Region A' });

// Mock the Region.findOne method to return the mock region
Region.findOne = jest.fn().mockResolvedValue(region);

// Call the getSiblings function
const siblings = getSiblings('Region A');

// Assert that the result is an array of sibling regions
expect(Array.isArray(siblings)).toBe(true);
expect(siblings).toHaveLength(2);
expect(siblings).toContainEqual({ name: 'Region B' });
expect(siblings).toContainEqual({ name: 'Region C' });
});

// Test case for when the region does not exist
test('should return an empty array when the region does not exist', () => {
// Mock the Region.findOne method to return null
Region.findOne = jest.fn().mockResolvedValue(null);

// Call the getSiblings function
const siblings = getSiblings('Region D');

// Assert that the result is an empty array
expect(Array.isArray(siblings)).toBe(true);
expect(siblings).toHaveLength(0);
});
});
Loading