Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 8623875

Browse files
committed
test: add unit tests for safeGet
1 parent bf8ab2d commit 8623875

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

projectHelpers.spec.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getIndentationCharacter, writePackageJson } = require("./projectHelpers");
1+
const { getIndentationCharacter, writePackageJson, safeGet } = require("./projectHelpers");
22
const fs = require("fs");
33

44
describe('projectHelpers', () => {
@@ -57,6 +57,28 @@ describe('projectHelpers', () => {
5757
});
5858
});
5959

60+
describe('safeGet', () => {
61+
it('should return the correct value of existing properties', () => {
62+
const obj = { a: 15 };
63+
expect(safeGet(obj, 'a')).toBe(15);
64+
});
65+
66+
it('should return undefined for unexisting properties', () => {
67+
const obj = { a: 15 };
68+
expect(safeGet(obj, 'random')).toBeUndefined();
69+
});
70+
71+
it('should return undefined when the first argument is undefined', () => {
72+
let obj;
73+
expect(safeGet(obj, 'random')).toBeUndefined();
74+
});
75+
76+
it('should return undefined when the first argument is not an object and does not have inherited property with the queried name', () => {
77+
const num = 15;
78+
expect(safeGet(num, 'random')).toBeUndefined();
79+
});
80+
});
81+
6082
describe('writePackageJson', () => {
6183
const mockFileSystemApi = () => {
6284
const data = {

0 commit comments

Comments
 (0)