Skip to content

Commit 2641d38

Browse files
committed
test: add test for 0001_two_sum.js
1 parent f734f86 commit 2641d38

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

js_solutions/0001_two_sum.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @param {Number} target
66
* @returns {Object []}
77
*/
8-
const twoSum = (nums, target) => {
8+
export default (nums, target) => {
99
// key: Number that adds up to target
1010
// value: Index the number is at
1111
const twoSum = {};

package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"homepage": "https://github.com/Saimon398/Problem-Solution#readme",
2020
"devDependencies": {
21+
"@jest/globals": "^29.2.2",
2122
"eslint": "^8.26.0",
2223
"eslint-config-airbnb-base": "^15.0.0",
2324
"eslint-plugin-import": "^2.26.0",

tests/0001_two_sum.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, test, expect } from '@jest/globals';
2+
import twoSum from '../js_solutions/0001_two_sum.js';
3+
4+
describe('Testing twoSum function', () => {
5+
test('Testing with different values', () => {
6+
// Should return indices of the two numbers such that they add up to target.
7+
expect(twoSum([2, 7, 11, 15], 9)).toEqual(expect.arrayContaining([0, 1]));
8+
expect(twoSum([3, 2, 4], 6)).toEqual(expect.arrayContaining([1, 2]));
9+
});
10+
test('Testing with the same values', () => {
11+
expect(twoSum([3, 3], 6)).toEqual(expect.arrayContaining([0, 1]));
12+
expect(twoSum([4, 4], 8)).toEqual(expect.arrayContaining([0, 1]));
13+
});
14+
// Should return -1, b/c there is no solution or solution is not the only one
15+
test('Testing with the absence of solution', () => {
16+
expect(twoSum([1, 2, 5], 4)).toEqual(-1);
17+
expect(twoSum([4, 4, 4, 4], 16)).toEqual(-1);
18+
});
19+
});

0 commit comments

Comments
 (0)