|
1 | 1 | import * as t from "io-ts"; |
| 2 | +import {Overrides, Request} from "puppeteer"; |
2 | 3 | import * as winston from "winston"; |
3 | | -import {createApi} from ".."; |
| 4 | +import {createApi, DType, Instagram, IPlugin} from ".."; |
4 | 5 | import {plugins} from ".."; |
5 | 6 | import {IOptions, IOptionsFullApi} from "../src/api/api"; |
6 | 7 | import {FakePage, IFakePageOptions} from "./__fixtures__/FakePage"; |
@@ -529,21 +530,28 @@ describe("Search", () => { |
529 | 530 | }); |
530 | 531 |
|
531 | 532 | 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 | + |
532 | 547 | const search = createApi( |
533 | 548 | "search", |
534 | 549 | "A really long long long string to find something in Instagram", |
| 550 | + { |
| 551 | + plugins: [new RequestCounter()], |
| 552 | + }, |
535 | 553 | ); |
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 | + |
547 | 555 | await search.get(); |
548 | 556 | expect(searchRequestsSpy).toBeCalledTimes(1); |
549 | 557 | }); |
|
0 commit comments