Skip to content

Commit 4c53a79

Browse files
test: write test cases for methods in array class
1 parent d484a01 commit 4c53a79

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
roots: ['<rootDir>/src/'],
5+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"scripts": {
1010
"build": "tsc --watch",
1111
"start": "nodemon dist/index.js",
12-
"dev": "concurrently \"yarn:build\" \"yarn:start\""
12+
"dev": "concurrently \"yarn:build\" \"yarn:start\"",
13+
"test": "jest --watchAll --no-cache"
1314
},
1415
"husky": {
1516
"hooks": {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { myArray } from '../arrayClass';
2+
3+
describe('Array Class Implementation', () => {
4+
test('new empty array is created', () => {
5+
const array = new myArray();
6+
expect(array.isEmpty()).toBe(true);
7+
});
8+
9+
test('create new array with 5 elements', () => {
10+
const array = new myArray(1, 2, 3, 4, 5);
11+
expect(array.size()).toEqual(5);
12+
});
13+
14+
test('get value from a given index in array', () => {
15+
const array = new myArray(
16+
{ id: 1, name: 'Francis' },
17+
{ id: 2, name: 'Carole' }
18+
);
19+
expect(array.get(1)).toEqual({ id: 2, name: 'Carole' });
20+
});
21+
22+
test('push value to end of array', () => {
23+
const array = new myArray();
24+
expect(array.push('Hello')).toBe(1);
25+
expect(array.push('there')).toBe(2);
26+
});
27+
28+
test('remove last item in array', () => {
29+
const array = new myArray(
30+
['name', 'Francis'],
31+
['name', 'Erika'],
32+
['name', 'Peter']
33+
);
34+
expect(array.pop()).toEqual(['name', 'Peter']);
35+
});
36+
37+
test('delete item at a given index of array', () => {
38+
const array = new myArray(
39+
{ id: 1, name: 'Francis' },
40+
{ id: 2, name: 'Carole' },
41+
{ id: 3, name: 'Peter' },
42+
{ id: 4, name: 'Erika' }
43+
);
44+
expect(array.delete(2)).toEqual({ id: 3, name: 'Peter' });
45+
});
46+
});

src/arrays/arrayClass.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
class myArray<T> {
1+
export class myArray<T> {
22
private length: number;
33
private data: {};
44

5-
public constructor(args?: T[]) {
5+
public constructor(...args: T[]) {
66
this.length = args.length;
77
this.data = {};
88
for (const key in args) {
@@ -18,7 +18,7 @@ class myArray<T> {
1818
return this.length === 0;
1919
}
2020

21-
public get(index: number): number {
21+
public get(index: number): T {
2222
return this.data[index];
2323
}
2424

0 commit comments

Comments
 (0)