Skip to content

Commit a6560f0

Browse files
committed
Refactored 12 to be more understandable
1 parent d125ce3 commit a6560f0

File tree

3 files changed

+137
-125
lines changed

3 files changed

+137
-125
lines changed
Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,60 @@
1-
import { expect, it } from "vitest";
1+
import { expect, it, describe } from "vitest";
22
import { Equal, Expect } from "../helpers/type-utils";
33

4-
const appConfig = {
5-
apiEndpoint: "https://api.example.com",
6-
apiVersion: "v1",
7-
apiKey: "1234567890",
8-
rawConfig: {
9-
featureFlags: {
10-
homePage: {
11-
showBanner: true,
12-
showLogOut: false,
13-
},
14-
loginPage: {
15-
showCaptcha: true,
16-
showConfirmPassword: false,
17-
},
18-
},
19-
},
20-
};
21-
22-
export const getFeatureFlags = (
4+
export const getHomePageFeatureFlags = (
235
config: unknown,
24-
override: (flags: unknown) => unknown,
6+
override: (flags: unknown) => unknown
257
) => {
268
return override(config.rawConfig.featureFlags.homePage);
279
};
2810

29-
it("Should return the homePage flag object", () => {
30-
const flags = getFeatureFlags(appConfig, (flags) => flags);
31-
32-
expect(flags).toEqual({
33-
showBanner: true,
34-
showLogOut: false,
11+
describe("getHomePageFeatureFlags", () => {
12+
const EXAMPLE_CONFIG = {
13+
apiEndpoint: "https://api.example.com",
14+
apiVersion: "v1",
15+
apiKey: "1234567890",
16+
rawConfig: {
17+
featureFlags: {
18+
homePage: {
19+
showBanner: true,
20+
showLogOut: false,
21+
},
22+
loginPage: {
23+
showCaptcha: true,
24+
showConfirmPassword: false,
25+
},
26+
},
27+
},
28+
};
29+
it("Should return the homePage flag object", () => {
30+
const flags = getHomePageFeatureFlags(
31+
EXAMPLE_CONFIG,
32+
(defaultFlags) => defaultFlags
33+
);
34+
35+
expect(flags).toEqual({
36+
showBanner: true,
37+
showLogOut: false,
38+
});
39+
40+
type tests = [
41+
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>
42+
];
3543
});
3644

37-
type tests = [
38-
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>,
39-
];
40-
});
45+
it("Should allow you to modify the result", () => {
46+
const flags = getHomePageFeatureFlags(EXAMPLE_CONFIG, (defaultFlags) => ({
47+
...defaultFlags,
48+
showBanner: false,
49+
}));
4150

42-
it("Should allow you to modify the result", () => {
43-
const flags = getFeatureFlags(appConfig, (flags) => ({
44-
...flags,
45-
showBanner: false,
46-
}));
51+
expect(flags).toEqual({
52+
showBanner: false,
53+
showLogOut: false,
54+
});
4755

48-
expect(flags).toEqual({
49-
showBanner: false,
50-
showLogOut: false,
56+
type tests = [
57+
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>
58+
];
5159
});
52-
53-
type tests = [
54-
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>,
55-
];
5660
});
Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
1-
import { expect, it } from "vitest";
1+
import { expect, it, describe } from "vitest";
22
import { Equal, Expect } from "../helpers/type-utils";
33

4-
const appConfig = {
5-
apiEndpoint: "https://api.example.com",
6-
apiVersion: "v1",
7-
apiKey: "1234567890",
8-
rawConfig: {
9-
featureFlags: {
10-
homePage: {
11-
showBanner: true,
12-
showLogOut: false,
13-
},
14-
loginPage: {
15-
showCaptcha: true,
16-
showConfirmPassword: false,
17-
},
18-
},
19-
},
20-
};
21-
22-
export const getFeatureFlags = <
4+
export const getHomePageFeatureFlags = <
235
TConfig extends {
246
rawConfig: {
257
featureFlags: {
@@ -36,31 +18,53 @@ export const getFeatureFlags = <
3618
return override(config.rawConfig.featureFlags.homePage);
3719
};
3820

39-
it("Should return the homePage flag object", () => {
40-
const flags = getFeatureFlags(appConfig, (flags) => flags);
21+
describe("getHomePageFeatureFlags", () => {
22+
const EXAMPLE_CONFIG = {
23+
apiEndpoint: "https://api.example.com",
24+
apiVersion: "v1",
25+
apiKey: "1234567890",
26+
rawConfig: {
27+
featureFlags: {
28+
homePage: {
29+
showBanner: true,
30+
showLogOut: false,
31+
},
32+
loginPage: {
33+
showCaptcha: true,
34+
showConfirmPassword: false,
35+
},
36+
},
37+
},
38+
};
39+
it("Should return the homePage flag object", () => {
40+
const flags = getHomePageFeatureFlags(
41+
EXAMPLE_CONFIG,
42+
(defaultFlags) => defaultFlags
43+
);
44+
45+
expect(flags).toEqual({
46+
showBanner: true,
47+
showLogOut: false,
48+
});
4149

42-
expect(flags).toEqual({
43-
showBanner: true,
44-
showLogOut: false,
50+
type tests = [
51+
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>
52+
];
4553
});
4654

47-
type tests = [
48-
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>
49-
];
50-
});
55+
it("Should allow you to modify the result", () => {
56+
const flags = getHomePageFeatureFlags(EXAMPLE_CONFIG, (defaultFlags) => ({
57+
...defaultFlags,
58+
showBanner: false,
59+
}));
5160

52-
it("Should allow you to modify the result", () => {
53-
const flags = getFeatureFlags(appConfig, (flags) => ({
54-
...flags,
55-
showBanner: false,
56-
}));
61+
expect(flags).toEqual({
62+
showBanner: false,
63+
showLogOut: false,
64+
});
5765

58-
expect(flags).toEqual({
59-
showBanner: false,
60-
showLogOut: false,
66+
type tests = [
67+
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>
68+
];
6169
});
62-
63-
type tests = [
64-
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>
65-
];
6670
});
Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
1-
import { expect, it } from "vitest";
1+
import { describe, expect, it } from "vitest";
22
import { Equal, Expect } from "../helpers/type-utils";
33

4-
const appConfig = {
5-
apiEndpoint: "https://api.example.com",
6-
apiVersion: "v1",
7-
apiKey: "1234567890",
8-
rawConfig: {
9-
featureFlags: {
10-
homePage: {
11-
showBanner: true,
12-
showLogOut: false,
13-
},
14-
loginPage: {
15-
showCaptcha: true,
16-
showConfirmPassword: false,
17-
},
18-
},
19-
},
20-
};
21-
22-
export const getFeatureFlags = <HomePageFlags>(
4+
export const getHomePageFeatureFlags = <HomePageFlags>(
235
config: {
246
rawConfig: {
257
featureFlags: {
@@ -32,31 +14,53 @@ export const getFeatureFlags = <HomePageFlags>(
3214
return override(config.rawConfig.featureFlags.homePage);
3315
};
3416

35-
it("Should return the homePage flag object", () => {
36-
const flags = getFeatureFlags(appConfig, (flags) => flags);
17+
describe("getHomePageFeatureFlags", () => {
18+
const EXAMPLE_CONFIG = {
19+
apiEndpoint: "https://api.example.com",
20+
apiVersion: "v1",
21+
apiKey: "1234567890",
22+
rawConfig: {
23+
featureFlags: {
24+
homePage: {
25+
showBanner: true,
26+
showLogOut: false,
27+
},
28+
loginPage: {
29+
showCaptcha: true,
30+
showConfirmPassword: false,
31+
},
32+
},
33+
},
34+
};
35+
it("Should return the homePage flag object", () => {
36+
const flags = getHomePageFeatureFlags(
37+
EXAMPLE_CONFIG,
38+
(defaultFlags) => defaultFlags
39+
);
3740

38-
expect(flags).toEqual({
39-
showBanner: true,
40-
showLogOut: false,
41+
expect(flags).toEqual({
42+
showBanner: true,
43+
showLogOut: false,
44+
});
45+
46+
type tests = [
47+
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>
48+
];
4149
});
4250

43-
type tests = [
44-
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>
45-
];
46-
});
51+
it("Should allow you to modify the result", () => {
52+
const flags = getHomePageFeatureFlags(EXAMPLE_CONFIG, (defaultFlags) => ({
53+
...defaultFlags,
54+
showBanner: false,
55+
}));
4756

48-
it("Should allow you to modify the result", () => {
49-
const flags = getFeatureFlags(appConfig, (flags) => ({
50-
...flags,
51-
showBanner: false,
52-
}));
57+
expect(flags).toEqual({
58+
showBanner: false,
59+
showLogOut: false,
60+
});
5361

54-
expect(flags).toEqual({
55-
showBanner: false,
56-
showLogOut: false,
62+
type tests = [
63+
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>
64+
];
5765
});
58-
59-
type tests = [
60-
Expect<Equal<typeof flags, { showBanner: boolean; showLogOut: boolean }>>
61-
];
6266
});

0 commit comments

Comments
 (0)