2022-06-15 17:14:05 +03:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-08-02 16:10:43 +03:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-06-15 17:14:05 +03:00
|
|
|
import { mount } from "enzyme";
|
|
|
|
import { sleep } from "matrix-js-sdk/src/utils";
|
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { act } from "react-dom/test-utils";
|
|
|
|
|
|
|
|
import { useLatestResult } from "../../src/hooks/useLatestResult";
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
function LatestResultsComponent({ query, doRequest }: { query: number; doRequest(query: number): Promise<number> }) {
|
2022-06-15 17:14:05 +03:00
|
|
|
const [value, setValueInternal] = useState<number>(0);
|
|
|
|
const [updateQuery, updateResult] = useLatestResult(setValueInternal);
|
|
|
|
useEffect(() => {
|
|
|
|
updateQuery(query);
|
2023-02-13 14:39:16 +03:00
|
|
|
doRequest(query).then((it: number) => {
|
2022-06-15 17:14:05 +03:00
|
|
|
updateResult(query, it);
|
|
|
|
});
|
|
|
|
}, [doRequest, query, updateQuery, updateResult]);
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
return <div>{value}</div>;
|
2022-06-15 17:14:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
describe("useLatestResult", () => {
|
|
|
|
it("should return results", async () => {
|
2023-02-13 14:39:16 +03:00
|
|
|
const doRequest = async (query: number) => {
|
2022-08-25 23:49:39 +03:00
|
|
|
await sleep(180);
|
2022-06-15 17:14:05 +03:00
|
|
|
return query;
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = mount(<LatestResultsComponent query={0} doRequest={doRequest} />);
|
|
|
|
await act(async () => {
|
2022-08-25 23:49:39 +03:00
|
|
|
await sleep(100);
|
2022-06-15 17:14:05 +03:00
|
|
|
});
|
2022-06-28 16:29:01 +03:00
|
|
|
expect(wrapper.text()).toEqual("0");
|
2022-06-15 17:14:05 +03:00
|
|
|
wrapper.setProps({ doRequest, query: 1 });
|
|
|
|
await act(async () => {
|
2022-08-25 23:49:39 +03:00
|
|
|
await sleep(70);
|
2022-06-15 17:14:05 +03:00
|
|
|
});
|
|
|
|
wrapper.setProps({ doRequest, query: 2 });
|
|
|
|
await act(async () => {
|
2022-08-25 23:49:39 +03:00
|
|
|
await sleep(70);
|
2022-06-15 17:14:05 +03:00
|
|
|
});
|
2022-06-28 16:29:01 +03:00
|
|
|
expect(wrapper.text()).toEqual("0");
|
2022-06-15 17:14:05 +03:00
|
|
|
await act(async () => {
|
2022-08-25 23:49:39 +03:00
|
|
|
await sleep(120);
|
2022-06-15 17:14:05 +03:00
|
|
|
});
|
2022-06-28 16:29:01 +03:00
|
|
|
expect(wrapper.text()).toEqual("2");
|
2022-06-15 17:14:05 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should prevent out-of-order results", async () => {
|
2023-02-13 14:39:16 +03:00
|
|
|
const doRequest = async (query: number) => {
|
2022-06-15 17:14:05 +03:00
|
|
|
await sleep(query);
|
|
|
|
return query;
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = mount(<LatestResultsComponent query={0} doRequest={doRequest} />);
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(5);
|
|
|
|
});
|
2022-06-28 16:29:01 +03:00
|
|
|
expect(wrapper.text()).toEqual("0");
|
2022-06-15 17:14:05 +03:00
|
|
|
wrapper.setProps({ doRequest, query: 50 });
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(5);
|
|
|
|
});
|
|
|
|
wrapper.setProps({ doRequest, query: 1 });
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(5);
|
|
|
|
});
|
2022-06-28 16:29:01 +03:00
|
|
|
expect(wrapper.text()).toEqual("1");
|
2022-06-15 17:14:05 +03:00
|
|
|
await act(async () => {
|
|
|
|
await sleep(50);
|
|
|
|
});
|
2022-06-28 16:29:01 +03:00
|
|
|
expect(wrapper.text()).toEqual("1");
|
2022-06-15 17:14:05 +03:00
|
|
|
});
|
|
|
|
});
|