Skip to content

Commit c6854b3

Browse files
authored
fix(desktop): fix time limit checkbox (#19)
* test(desktop): send empty measure to reflect reality * test(desktop): go for maximum coverage Also add a failing test for time limit checkbox bug * fix(desktop): fix time limit checbox Measures were stopped even if the checkbox was unchecked * chore(desktop): bump version
1 parent 538e597 commit c6854b3

File tree

4 files changed

+99
-39
lines changed

4 files changed

+99
-39
lines changed

flipper-desktop/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "https://fbflipper.com/schemas/plugin-package/v2.json",
33
"name": "flipper-plugin-rn-perf-monitor",
44
"id": "rn-perf-monitor",
5-
"version": "0.4.0",
5+
"version": "0.4.1",
66
"main": "dist/bundle.js",
77
"flipperBundlerEntry": "src/index.tsx",
88
"license": "MIT",

flipper-desktop/src/PerfMonitorView.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ const TimeLimitControl = ({
5454
<FormControlLabel
5555
control={
5656
<Checkbox
57+
inputProps={{
58+
"aria-label": "Time limit enabled",
59+
}}
5760
checked={timeLimitEnabled}
5861
onChange={(event: { target: { checked: boolean } }) => {
5962
toggleTimeLimit(event.target.checked);
@@ -63,6 +66,9 @@ const TimeLimitControl = ({
6366
label="Enable time limit of "
6467
/>
6568
<TextField
69+
inputProps={{
70+
"aria-label": "Time limit",
71+
}}
6672
type="number"
6773
onChange={({ target: { value } }) =>
6874
setTimeLimit(Math.floor(parseInt(value, 10)))
@@ -96,7 +102,11 @@ export const PerfMonitorView = ({
96102
const [timeLimitEnabled, setTimeLimitEnabled] = useState(true);
97103
const [timeLimit, setTimeLimit] = useState<number | null>(DEFAULT_TIME_LIMIT);
98104
useEffect(() => {
99-
if (timeLimit && measures.length > timeLimit / MEASURE_INTERVAL) {
105+
if (
106+
timeLimitEnabled &&
107+
timeLimit &&
108+
measures.length > timeLimit / MEASURE_INTERVAL
109+
) {
100110
stopMeasuring();
101111
}
102112
}, [timeLimit, measures]);

flipper-desktop/src/__tests__/Plugin.spec.tsx

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,96 @@ require("@testing-library/react");
1111
jest.mock("react-apexcharts", () => "apex-charts");
1212
jest.mock("apexcharts", () => ({ exec: jest.fn() }));
1313

14-
test("displays FPS data and scoring", async () => {
15-
const { renderer, sendEvent, onSend } = TestUtils.renderPlugin(Plugin);
16-
17-
fireEvent.click(renderer.getByText("Start Measuring"));
18-
expect(onSend).toHaveBeenCalledWith("startMeasuring", undefined);
14+
const setupPlugin = () => {
15+
const { instance, renderer, sendEvent, onSend } =
16+
TestUtils.renderPlugin(Plugin);
1917

20-
sendEvent("addRecord", {
21-
JS: 30,
22-
UI: 25,
23-
expected: 30,
24-
});
18+
// First measure on Android is always 0 and is ignored by plugin
2519
sendEvent("addRecord", {
2620
JS: 0,
27-
UI: 30,
21+
UI: 0,
2822
expected: 30,
2923
});
3024

25+
return {
26+
addMeasure: ({ JS, UI }: { JS: number; UI: number }) =>
27+
sendEvent("addRecord", {
28+
JS,
29+
UI,
30+
expected: 30,
31+
}),
32+
clickStart: () => fireEvent.click(renderer.getByText("Start Measuring")),
33+
clickStop: () => fireEvent.click(renderer.getByText("Stop Measuring")),
34+
expectToMatchSnapshot: () => {
35+
expect(
36+
(renderer.baseElement as HTMLBodyElement).textContent
37+
).toMatchSnapshot();
38+
expect(renderer.baseElement).toMatchSnapshot();
39+
},
40+
setTimeLimit: (limit: number) => {
41+
const input = renderer.getByLabelText("Time limit");
42+
fireEvent.change(input, { target: { value: limit } });
43+
},
44+
clickTimeLimitCheckbox: () => {
45+
const checkbox = renderer.getByLabelText("Time limit enabled");
46+
fireEvent.click(checkbox);
47+
},
48+
49+
renderer,
50+
instance,
51+
onSend,
52+
};
53+
};
54+
55+
test("displays FPS data and scoring", async () => {
56+
const { addMeasure, expectToMatchSnapshot } = setupPlugin();
57+
58+
addMeasure({ JS: 30, UI: 25 });
59+
addMeasure({ JS: 0, UI: 30 });
60+
61+
expectToMatchSnapshot();
62+
});
63+
64+
test("clicking start should reset measures and start measures", () => {
65+
const { addMeasure, clickStart, clickStop, instance, onSend } = setupPlugin();
66+
67+
addMeasure({ JS: 30, UI: 25 });
68+
addMeasure({ JS: 0, UI: 30 });
69+
70+
clickStart();
71+
expect(onSend).toHaveBeenCalledWith("startMeasuring", undefined);
3172
onSend.mockClear();
3273

33-
fireEvent.click(renderer.getByText("Stop Measuring"));
74+
expect(instance.measures.get()).toEqual([]);
75+
76+
clickStop();
3477
expect(onSend).toHaveBeenCalledWith("stopMeasuring", undefined);
78+
});
3579

36-
expect(
37-
(renderer.baseElement as HTMLBodyElement).textContent
38-
).toMatchSnapshot();
39-
expect(renderer.baseElement).toMatchSnapshot();
80+
test("stops after set time limit", () => {
81+
const { addMeasure, setTimeLimit, onSend } = setupPlugin();
82+
83+
setTimeLimit(1000);
84+
85+
addMeasure({ JS: 30, UI: 30 });
86+
addMeasure({ JS: 30, UI: 30 });
87+
expect(onSend).not.toHaveBeenCalledWith("stopMeasuring", undefined);
88+
// 1000ms means 3 measures would be displayed: 0, 500 and 1000
89+
addMeasure({ JS: 30, UI: 30 });
90+
expect(onSend).toHaveBeenCalledWith("stopMeasuring", undefined);
4091
});
4192

42-
test("clicking start should reset measures", () => {
43-
const { renderer, sendEvent, instance } = TestUtils.renderPlugin(Plugin);
93+
test("continues after set time limit if time limit disabled", () => {
94+
const { addMeasure, clickTimeLimitCheckbox, setTimeLimit, onSend } =
95+
setupPlugin();
4496

45-
sendEvent("addRecord", {
46-
JS: 30,
47-
UI: 25,
48-
expected: 30,
49-
});
50-
sendEvent("addRecord", {
51-
JS: 0,
52-
UI: 30,
53-
expected: 30,
54-
});
97+
setTimeLimit(1000);
98+
clickTimeLimitCheckbox();
5599

56-
fireEvent.click(renderer.getByText("Start Measuring"));
57-
expect(instance.measures.get()).toEqual([]);
100+
addMeasure({ JS: 30, UI: 30 });
101+
addMeasure({ JS: 30, UI: 30 });
102+
expect(onSend).not.toHaveBeenCalledWith("stopMeasuring", undefined);
103+
// 1000ms means 3 measures would be displayed: 0, 500 and 1000
104+
addMeasure({ JS: 30, UI: 30 });
105+
expect(onSend).not.toHaveBeenCalledWith("stopMeasuring", undefined);
58106
});

flipper-desktop/src/__tests__/__snapshots__/Plugin.spec.tsx.snap

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`displays FPS data and scoring 1`] = `"Performance0Average JS FPS0Average UI FPS60JS threadlock0.506s (100%)Start MeasuringEnable time limit of ms"`;
3+
exports[`displays FPS data and scoring 1`] = `"Performance35Average JS FPS30Average UI FPS55JS threadlock0.506s (50%)Start MeasuringEnable time limit of ms"`;
44

55
exports[`displays FPS data and scoring 2`] = `
66
<body>
@@ -21,7 +21,7 @@ exports[`displays FPS data and scoring 2`] = `
2121
class="MuiBox-root MuiBox-root-1"
2222
>
2323
<div
24-
aria-valuenow="0"
24+
aria-valuenow="35"
2525
class="MuiCircularProgress-root MuiCircularProgress-colorPrimary MuiCircularProgress-determinate"
2626
role="progressbar"
2727
style="width: 80px; height: 80px; transform: rotate(-90deg); color: rgb(255, 65, 54);"
@@ -37,7 +37,7 @@ exports[`displays FPS data and scoring 2`] = `
3737
fill="none"
3838
r="20.2"
3939
stroke-width="3.6"
40-
style="stroke-dasharray: 126.920; stroke-dashoffset: 126.920px;"
40+
style="stroke-dasharray: 126.920; stroke-dashoffset: 82.498px;"
4141
/>
4242
</svg>
4343
</div>
@@ -48,7 +48,7 @@ exports[`displays FPS data and scoring 2`] = `
4848
class="MuiTypography-root MuiTypography-caption MuiTypography-colorTextSecondary"
4949
style="font-size: 28px; color: rgb(255, 65, 54);"
5050
>
51-
0
51+
35
5252
</div>
5353
</div>
5454
</div>
@@ -73,7 +73,7 @@ exports[`displays FPS data and scoring 2`] = `
7373
<td
7474
class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignRight"
7575
>
76-
0
76+
30
7777
</td>
7878
</tr>
7979
<tr
@@ -89,7 +89,7 @@ exports[`displays FPS data and scoring 2`] = `
8989
<td
9090
class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignRight"
9191
>
92-
60
92+
55
9393
</td>
9494
</tr>
9595
<tr
@@ -105,7 +105,7 @@ exports[`displays FPS data and scoring 2`] = `
105105
<td
106106
class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignRight"
107107
>
108-
0.506s (100%)
108+
0.506s (50%)
109109
</td>
110110
</tr>
111111
</tbody>
@@ -155,6 +155,7 @@ exports[`displays FPS data and scoring 2`] = `
155155
class="MuiIconButton-label"
156156
>
157157
<input
158+
aria-label="Time limit enabled"
158159
checked=""
159160
class="PrivateSwitchBase-input-6"
160161
data-indeterminate="false"
@@ -190,6 +191,7 @@ exports[`displays FPS data and scoring 2`] = `
190191
>
191192
<input
192193
aria-invalid="false"
194+
aria-label="Time limit"
193195
class="MuiInputBase-input MuiInput-input"
194196
type="number"
195197
value="10000"

0 commit comments

Comments
 (0)