Skip to content

Commit 2067f95

Browse files
authored
Fix/singleton test (#9)
* start of singleton test fix * works again * some polish for singleton tests * complete note * fix linter stuff
1 parent cc80d8b commit 2067f95

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

test/__common__/ftProps.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
3+
const { FeatureToggles } = jest.requireActual("../../src/featureToggles");
4+
const singleton = jest.requireActual("../../src/singleton");
5+
6+
const IGNORE_PROPERTIES = ["constructor", "length", "name", "prototype", "getInstance"];
7+
8+
const ftInstanceProps = Object.getOwnPropertyNames(FeatureToggles.prototype).filter(
9+
(prop) => !IGNORE_PROPERTIES.includes(prop) && !prop.startsWith("_")
10+
);
11+
const ftClassProps = Object.getOwnPropertyNames(FeatureToggles).filter(
12+
(prop) => !IGNORE_PROPERTIES.includes(prop) && !prop.startsWith("_")
13+
);
14+
const ftProps = [].concat(ftClassProps, ftInstanceProps);
15+
16+
const singletonProps = Object.keys(singleton).filter((p) => !p.startsWith("_"));
17+
18+
module.exports = {
19+
IGNORE_PROPERTIES,
20+
21+
ftInstanceProps,
22+
ftClassProps,
23+
ftProps,
24+
singletonProps,
25+
};

test/singleton.test.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
11
"use strict";
22

3-
const { FeatureToggles } = require("../src/featureToggles");
4-
const singleton = require("../src/singleton");
53
const { ENV } = require("../src/shared/static");
64

75
describe("singleton test", () => {
86
beforeEach(() => {
97
jest.clearAllMocks();
108
});
119

12-
it("singleton correctly exposes public apis of feature-toggles", async () => {
13-
const IGNORE_PROPERTIES = ["constructor", "length", "name", "prototype", "getInstance"];
14-
15-
// check same properties
16-
const singletonProps = Object.keys(singleton).filter((p) => !p.startsWith("_"));
17-
const ftInstanceProps = Object.getOwnPropertyNames(FeatureToggles.prototype).filter(
18-
(prop) => prop !== "constructor" && !prop.startsWith("_")
19-
);
20-
const ftClassProps = Object.getOwnPropertyNames(FeatureToggles).filter(
21-
(prop) => !IGNORE_PROPERTIES.includes(prop) && !prop.startsWith("_")
22-
);
23-
const ftProps = [].concat(ftClassProps, ftInstanceProps);
10+
it("singleton exposes same public apis as feature-toggles", async () => {
11+
const { ftProps, singletonProps } = require("./__common__/ftProps");
2412

2513
const sameLength = ftProps.length === singletonProps.length;
2614
const mismatch = ftProps.find((p, i) => p !== singletonProps[i]);
27-
2815
expect(sameLength).toBe(true);
2916
expect(mismatch).toBe(undefined);
3017
});

test/singletonWithMock.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"use strict";
2+
3+
const MockFeatureToggles = jest.fn();
4+
jest.mock("../src/featureToggles", () => {
5+
const { ftInstanceProps, ftClassProps } = require("./__common__/ftProps");
6+
ftInstanceProps.forEach((prop) => {
7+
MockFeatureToggles.prototype[prop] = jest.fn();
8+
});
9+
ftClassProps.forEach((prop) => {
10+
MockFeatureToggles[prop] = jest.fn();
11+
});
12+
let instance = new MockFeatureToggles();
13+
MockFeatureToggles.getInstance = () => instance;
14+
return {
15+
FeatureToggles: MockFeatureToggles,
16+
};
17+
});
18+
const singleton = require("../src/singleton");
19+
20+
describe("singleton test with feature toggles class mock", () => {
21+
beforeEach(() => {
22+
jest.clearAllMocks();
23+
});
24+
25+
it("singleton property mapping is correct", async () => {
26+
// NOTE: the singleton and FeatureToggles functions match up, but the mapping could be flipped, so we check that
27+
// any call to the singleton reaches the correct instance function of the FeatureToggles
28+
const { singletonProps } = require("./__common__/ftProps");
29+
30+
const inputs = ["input1", "input2"];
31+
const instance = MockFeatureToggles.getInstance();
32+
for (const prop of singletonProps) {
33+
const singletonFunc = singleton[prop];
34+
const instanceFunc = instance[prop] || MockFeatureToggles[prop];
35+
await singletonFunc(...inputs);
36+
expect(instanceFunc).toHaveBeenCalledTimes(1);
37+
expect(instanceFunc).toHaveBeenCalledWith(...inputs);
38+
}
39+
});
40+
});

0 commit comments

Comments
 (0)