Skip to content

Commit 8eda54e

Browse files
committed
test
1 parent 74316c5 commit 8eda54e

File tree

5 files changed

+134
-128
lines changed

5 files changed

+134
-128
lines changed

test/modal/test.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import process from 'node:process'
2+
import path from 'node:path'
3+
import { expect, test } from '@jest/globals'
4+
5+
import IXCrawl from '../../src'
6+
7+
const args = process.argv.slice(3)
8+
const currentModel = args[0]
9+
10+
let XCrawl: typeof IXCrawl
11+
if (currentModel === 'dev') {
12+
XCrawl = require('../../src').default
13+
} else if (currentModel === 'pro') {
14+
XCrawl = require('../../publish/dist')
15+
}
16+
17+
// Protocol
18+
19+
function httpProtocol() {
20+
return new Promise((resolve) => {
21+
const httpXCrawl = new XCrawl()
22+
23+
httpXCrawl
24+
.fetch({
25+
requestConifg: {
26+
url: 'http://localhost:9001/api/home/goodprice'
27+
}
28+
})
29+
.then(() => resolve(true))
30+
})
31+
}
32+
33+
function httpsProtocol() {
34+
return new Promise((resolve) => {
35+
const httpsXCrawl = new XCrawl()
36+
37+
httpsXCrawl
38+
.fetchHTML('https://docs.github.com/zh')
39+
.then(() => resolve(true))
40+
})
41+
}
42+
43+
test('http protocol', async () => {
44+
await expect(httpProtocol()).resolves.toBe(true)
45+
})
46+
47+
test('https protocol', async () => {
48+
await expect(httpsProtocol()).resolves.toBe(true)
49+
})
50+
51+
// API
52+
function fetchAPI() {
53+
return new Promise((resolve) => {
54+
const myXCrawl = new XCrawl()
55+
56+
myXCrawl
57+
.fetch({
58+
requestConifg: {
59+
url: 'http://localhost:9001/api/area/阳江市',
60+
method: 'POST',
61+
data: {
62+
type: 'goodPrice',
63+
offset: 0,
64+
size: 20
65+
}
66+
}
67+
})
68+
.then(() => resolve(true))
69+
})
70+
}
71+
72+
function fetchFileAPI() {
73+
return new Promise((resolve) => {
74+
const myXCrawl = new XCrawl({
75+
timeout: 10000,
76+
intervalTime: {
77+
max: 2000,
78+
min: 1000
79+
}
80+
})
81+
82+
myXCrawl
83+
.fetch({
84+
requestConifg: {
85+
url: 'http://localhost:9001/api/area/阳江市',
86+
method: 'POST',
87+
data: {
88+
type: 'goodPrice',
89+
offset: 0,
90+
size: 20
91+
}
92+
}
93+
})
94+
.then((res) => {
95+
const roomList = res[0].data.data.list
96+
const requestConifg = roomList.map((item: any) => ({
97+
url: item.coverUrl
98+
}))
99+
100+
myXCrawl
101+
.fetchFile({
102+
requestConifg,
103+
fileConfig: {
104+
storeDir: path.resolve(__dirname, './upload')
105+
}
106+
})
107+
.then(() => resolve(true))
108+
})
109+
})
110+
}
111+
112+
function fetchHTMLAPI() {
113+
return new Promise((resolve) => {
114+
const myXCrawl = new XCrawl()
115+
116+
myXCrawl.fetchHTML('https://docs.github.com/zh').then(() => resolve(true))
117+
})
118+
}
119+
120+
test('fetch API', async () => {
121+
await expect(fetchAPI()).resolves.toBe(true)
122+
})
123+
124+
test('fetchFile API', async () => {
125+
await expect(fetchFileAPI()).resolves.toBe(true)
126+
})
127+
128+
test('fetchHTML API', async () => {
129+
await expect(fetchHTMLAPI()).resolves.toBe(true)
130+
})

test/start/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/start/index.ts

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,5 @@
1-
import path from 'node:path'
2-
31
import XCrawl from '../../src'
42

5-
import { IRequestConfig } from '../../src/types'
6-
import { IAreaRoom, IBRecommend } from './types'
7-
8-
// 1. 房间数据
9-
// https://github.com/coder-hxl/airbnb-api
10-
11-
const roomXCrawl = new XCrawl({
12-
timeout: 10000,
13-
intervalTime: {
14-
max: 3000,
15-
min: 1000
16-
}
17-
})
18-
19-
function areaRoomData() {
20-
roomXCrawl
21-
.fetch<IAreaRoom>({
22-
requestConifg: {
23-
url: 'http://localhost:9001/api/area/阳江市',
24-
method: 'POST',
25-
data: {
26-
type: 'plus',
27-
offset: 0,
28-
size: 20
29-
}
30-
}
31-
})
32-
.then((areaData) => {
33-
const { id, pictureUrls } = areaData.data.list[0]
34-
console.log('areaData Id: ', id)
35-
36-
const requestConifg: IRequestConfig[] = pictureUrls.map((url) => ({
37-
url,
38-
method: 'GET'
39-
}))
40-
41-
roomXCrawl.fetchFile({
42-
requestConifg,
43-
fileConfig: { storeDir: path.resolve(__dirname, './upload') }
44-
})
45-
})
46-
}
47-
// areaRoomData()
48-
49-
// 2 HTML: GitHub Docs
50-
// 采用 jsdom 对 HTML String 解析
51-
523
const githubDocsXCrawl = new XCrawl({
534
timeout: 10000,
545
intervalTime: {
@@ -57,10 +8,6 @@ const githubDocsXCrawl = new XCrawl({
578
}
589
})
5910

60-
async function githubDocs() {
61-
const dom = await githubDocsXCrawl.fetchHTML('https://docs.github.com/zh')
62-
63-
console.log(dom.window.document.querySelector('title')?.textContent)
64-
}
65-
66-
// githubDocs()
11+
githubDocsXCrawl.fetchHTML('https://docs.github.com/zh').then((jsdom) => {
12+
console.log(jsdom.window.document.querySelector('title')?.textContent)
13+
})

test/start/types.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.

test/test.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)