mirror of
https://github.com/primedigitaltech/azon_seeker.git
synced 2026-10-05 07:13:06 +08:00
34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
import { JSDOM } from 'jsdom';
|
|
import { collectAmazonNextPageUrl } from '../src/page-worker/web-injectors/amazon-search-pagination';
|
|
|
|
const test = async (name: string, html: string, expected: string | null) => {
|
|
const dom = new JSDOM(html, { url: 'https://www.amazon.com/s?k=security+camera&page=1' });
|
|
const previousDocument = globalThis.document;
|
|
const previousWindow = globalThis.window;
|
|
Object.assign(globalThis, { document: dom.window.document, window: dom.window });
|
|
try {
|
|
const actual = await collectAmazonNextPageUrl();
|
|
if (actual !== expected) throw new Error(`${name}: expected ${expected}, received ${actual}`);
|
|
} finally {
|
|
Object.assign(globalThis, { document: previousDocument, window: previousWindow });
|
|
dom.window.close();
|
|
}
|
|
};
|
|
|
|
const main = async () => {
|
|
await test(
|
|
'active next page',
|
|
'<a class="s-pagination-next" href="/s?k=security+camera&page=2">Next</a>',
|
|
'https://www.amazon.com/s?k=security+camera&page=2',
|
|
);
|
|
await test(
|
|
'disabled next page',
|
|
'<span class="s-pagination-next s-pagination-disabled">Next</span>',
|
|
null,
|
|
);
|
|
await test('missing next page', '<main>No pagination</main>', null);
|
|
console.log('Amazon search pagination tests passed.');
|
|
};
|
|
|
|
void main();
|