Skip to content

Commit 8ec753f

Browse files
committed
Extract supported percentage calculation to separate file
1 parent e300b40 commit 8ec753f

File tree

3 files changed

+75
-14
lines changed

3 files changed

+75
-14
lines changed

extensions/ql-vscode/src/view/data-extensions-editor/DataExtensionsEditor.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ExternalApiUsage } from "../../data-extensions-editor/external-api-usag
1414
import { ModeledMethod } from "../../data-extensions-editor/modeled-method";
1515
import { MethodRow } from "./MethodRow";
1616
import { assertNever } from "../../pure/helpers-pure";
17+
import { calculateSupportedPercentage } from "./supported";
1718

1819
export const DataExtensionsEditorContainer = styled.div`
1920
margin-top: 1rem;
@@ -70,21 +71,12 @@ export function DataExtensionsEditor(): JSX.Element {
7071
};
7172
}, []);
7273

73-
const supportedPercentage = useMemo(() => {
74-
return (
75-
(externalApiUsages.filter((m) => m.supported).length /
76-
externalApiUsages.length) *
77-
100
78-
);
79-
}, [externalApiUsages]);
74+
const supportedPercentage = useMemo(
75+
() => calculateSupportedPercentage(externalApiUsages),
76+
[externalApiUsages],
77+
);
8078

81-
const unsupportedPercentage = useMemo(() => {
82-
return (
83-
(externalApiUsages.filter((m) => !m.supported).length /
84-
externalApiUsages.length) *
85-
100
86-
);
87-
}, [externalApiUsages]);
79+
const unsupportedPercentage = 100 - supportedPercentage;
8880

8981
const onChange = useCallback(
9082
(method: ExternalApiUsage, model: ModeledMethod) => {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { calculateSupportedPercentage } from "../supported";
2+
3+
describe("calculateSupportedPercentage", () => {
4+
it("when there are no external API usages", () => {
5+
expect(calculateSupportedPercentage([])).toBe(0);
6+
});
7+
8+
it("when there are is 1 supported external API usage", () => {
9+
expect(
10+
calculateSupportedPercentage([
11+
{
12+
supported: true,
13+
},
14+
]),
15+
).toBe(100);
16+
});
17+
18+
it("when there are is 1 unsupported external API usage", () => {
19+
expect(
20+
calculateSupportedPercentage([
21+
{
22+
supported: false,
23+
},
24+
]),
25+
).toBe(0);
26+
});
27+
28+
it("when there are multiple supporte and unsupported external API usage", () => {
29+
expect(
30+
calculateSupportedPercentage([
31+
{
32+
supported: false,
33+
},
34+
{
35+
supported: true,
36+
},
37+
{
38+
supported: false,
39+
},
40+
{
41+
supported: false,
42+
},
43+
{
44+
supported: true,
45+
},
46+
{
47+
supported: false,
48+
},
49+
]),
50+
).toBeCloseTo(33.33);
51+
});
52+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ExternalApiUsage } from "../../data-extensions-editor/external-api-usage";
2+
3+
export function calculateSupportedPercentage(
4+
externalApiUsages: Array<Pick<ExternalApiUsage, "supported">>,
5+
): number {
6+
if (externalApiUsages.length === 0) {
7+
return 0;
8+
}
9+
10+
const supportedExternalApiUsages = externalApiUsages.filter(
11+
(m) => m.supported,
12+
);
13+
14+
const supportedRatio =
15+
supportedExternalApiUsages.length / externalApiUsages.length;
16+
return supportedRatio * 100;
17+
}

0 commit comments

Comments
 (0)