Skip to content

Commit 5c748ad

Browse files
committed
use plugins to check for multiple requests
1 parent 9c403b9 commit 5c748ad

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

tests/test.spec.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as t from "io-ts";
2+
import {Overrides, Request} from "puppeteer";
23
import * as winston from "winston";
3-
import {createApi} from "..";
4+
import {createApi, DType, Instagram, IPlugin} from "..";
45
import {plugins} from "..";
56
import {IOptions, IOptionsFullApi} from "../src/api/api";
67
import {FakePage, IFakePageOptions} from "./__fixtures__/FakePage";
@@ -529,21 +530,28 @@ describe("Search", () => {
529530
});
530531

531532
test("Search should fire only one network request", async () => {
533+
const searchRequestsSpy = jest.fn();
534+
class RequestCounter implements IPlugin {
535+
public requestEvent(
536+
req: Request,
537+
overrides: Overrides,
538+
state: Instagram<DType>,
539+
): void {
540+
// @ts-ignore
541+
if (state.matchURL(req.url())) {
542+
searchRequestsSpy();
543+
}
544+
}
545+
}
546+
532547
const search = createApi(
533548
"search",
534549
"A really long long long string to find something in Instagram",
550+
{
551+
plugins: [new RequestCounter()],
552+
},
535553
);
536-
const searchRequestsSpy = jest.fn();
537-
await search.start();
538-
// @ts-ignore
539-
search.page.on("request", (event) => {
540-
const requestUrl = event.url();
541-
// @ts-ignore
542-
if (!search.matchURL(requestUrl)) {
543-
return;
544-
}
545-
searchRequestsSpy(event);
546-
});
554+
547555
await search.get();
548556
expect(searchRequestsSpy).toBeCalledTimes(1);
549557
});

0 commit comments

Comments
 (0)