Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5d7ffa6

Browse files
author
knight.chen
committed
feat: add test code for data source
1 parentf4478fc commit5d7ffa6

File tree

9 files changed

+433
-15
lines changed

9 files changed

+433
-15
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`fetch request > responseType 1`]=`
4+
[MockFunction spy]{
5+
"calls": [
6+
[
7+
"https://127.0.0.1/info.json",
8+
{
9+
"credentials":"include",
10+
"headers": {
11+
"Accept":"application/json",
12+
},
13+
"method":"GET",
14+
},
15+
],
16+
[
17+
"https://127.0.0.1/info.json",
18+
{
19+
"credentials":"include",
20+
"headers": {
21+
"Accept":"application/json",
22+
},
23+
"method":"GET",
24+
},
25+
],
26+
[
27+
"https://127.0.0.1/info.json?a=5&c=%7B%22d%22%3A5%7D",
28+
{
29+
"credentials":"include",
30+
"headers": {
31+
"Accept":"application/json",
32+
},
33+
"method":"GET",
34+
},
35+
],
36+
[
37+
"https://127.0.0.1/info.json",
38+
{
39+
"body": {
40+
"a":5,
41+
},
42+
"credentials":"include",
43+
"headers": {
44+
"Accept":"application/json",
45+
"content-type":"json",
46+
},
47+
"method":"POST",
48+
},
49+
],
50+
],
51+
"results": [
52+
{
53+
"type":"return",
54+
"value": {
55+
"json": [Function],
56+
"status":200,
57+
"statusText":"OK",
58+
},
59+
},
60+
{
61+
"type":"return",
62+
"value": {
63+
"json": [Function],
64+
"status":500,
65+
"statusText":"Server internal error",
66+
},
67+
},
68+
{
69+
"type":"return",
70+
"value": {
71+
"json": [Function],
72+
"status":200,
73+
"statusText":"OK",
74+
},
75+
},
76+
{
77+
"type":"return",
78+
"value": {
79+
"blob": [Function],
80+
"status":200,
81+
"statusText":"OK",
82+
},
83+
},
84+
],
85+
}
86+
`;
87+
88+
exports[`fetch request > with params 1`]=`
89+
[MockFunction spy]{
90+
"calls": [
91+
[
92+
"https://127.0.0.1/info.json",
93+
{
94+
"credentials":"include",
95+
"headers": {
96+
"Accept":"application/json",
97+
},
98+
"method":"GET",
99+
},
100+
],
101+
[
102+
"https://127.0.0.1/info.json",
103+
{
104+
"credentials":"include",
105+
"headers": {
106+
"Accept":"application/json",
107+
},
108+
"method":"GET",
109+
},
110+
],
111+
[
112+
"https://127.0.0.1/info.json?a=5&c=%7B%22d%22%3A5%7D",
113+
{
114+
"credentials":"include",
115+
"headers": {
116+
"Accept":"application/json",
117+
},
118+
"method":"GET",
119+
},
120+
],
121+
],
122+
"results": [
123+
{
124+
"type":"return",
125+
"value": {
126+
"json": [Function],
127+
"status":200,
128+
"statusText":"OK",
129+
},
130+
},
131+
{
132+
"type":"return",
133+
"value": {
134+
"json": [Function],
135+
"status":500,
136+
"statusText":"Server internal error",
137+
},
138+
},
139+
{
140+
"type":"return",
141+
"value": {
142+
"json": [Function],
143+
"status":200,
144+
"statusText":"OK",
145+
},
146+
},
147+
],
148+
}
149+
`;
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import{noop}from'@knxcloud/lowcode-utils';
2+
import{createDataSourceEngine}from'../src';
3+
4+
describe('data source engine',async()=>{
5+
constmockedFetch=vi.fn();
6+
7+
beforeEach(()=>{
8+
vi.stubGlobal('fetch',mockedFetch);
9+
});
10+
11+
afterEach(()=>{
12+
vi.unstubAllEnvs();
13+
});
14+
15+
test('nomal request',async()=>{
16+
mockedFetch.mockResolvedValueOnce({
17+
json:()=>Promise.resolve(1),
18+
status:200,
19+
statusText:'OK',
20+
});
21+
conststate:Record<string,unknown>={};
22+
constengine=createDataSourceEngine(
23+
{
24+
list:[
25+
{
26+
id:'info',
27+
options:()=>({
28+
uri:'http://127.0.0.1/info.json',
29+
method:'POST',
30+
}),
31+
},
32+
],
33+
},
34+
{
35+
state:state,
36+
setState(newState){
37+
Object.assign(state,newState);
38+
},
39+
forceUpdate:noop,
40+
}
41+
);
42+
constres=awaitengine.dataSourceMap.info.load();
43+
expect(res).eq(engine.dataSourceMap.info.data).eq(state.info).eq(1);
44+
45+
expect(mockedFetch).toHaveBeenCalledWith('http://127.0.0.1/info.json',{
46+
body:'{}',
47+
credentials:'include',
48+
headers:{
49+
Accept:'application/json',
50+
},
51+
method:'POST',
52+
});
53+
});
54+
55+
test('error request',async()=>{
56+
mockedFetch.mockResolvedValueOnce({
57+
json:()=>Promise.resolve(1),
58+
status:500,
59+
statusText:'Server Internal Error',
60+
});
61+
conststate:Record<string,unknown>={};
62+
constengine=createDataSourceEngine(
63+
{
64+
list:[
65+
{
66+
id:'info',
67+
options:()=>({
68+
uri:'http://127.0.0.1/info.json',
69+
method:'GET',
70+
}),
71+
},
72+
],
73+
},
74+
{
75+
state:state,
76+
setState(newState){
77+
Object.assign(state,newState);
78+
},
79+
forceUpdate:noop,
80+
}
81+
);
82+
expect(()=>engine.dataSourceMap.info.load()).rejects.toThrowError(
83+
'Server Internal Error'
84+
);
85+
expect(engine.dataSourceMap.info.error).toBeUndefined();
86+
});
87+
88+
test('should fetch',async()=>{
89+
mockedFetch.mockResolvedValueOnce({
90+
json:()=>Promise.resolve(1),
91+
status:200,
92+
statusText:'OK',
93+
});
94+
conststate:Record<string,unknown>={};
95+
constengine=createDataSourceEngine(
96+
{
97+
list:[
98+
{
99+
id:'info',
100+
options:()=>({
101+
uri:'http://127.0.0.1/info.json',
102+
method:'GET',
103+
}),
104+
shouldFetch:()=>false,
105+
},
106+
],
107+
},
108+
{
109+
state:state,
110+
setState(newState){
111+
Object.assign(state,newState);
112+
},
113+
forceUpdate:noop,
114+
}
115+
);
116+
117+
expect(()=>engine.dataSourceMap.info.load()).rejects.toThrowError();
118+
});
119+
120+
test('will fetch',async()=>{
121+
mockedFetch.mockResolvedValueOnce({
122+
json:()=>Promise.resolve(1),
123+
status:200,
124+
statusText:'OK',
125+
});
126+
conststate:Record<string,unknown>={};
127+
constengine=createDataSourceEngine(
128+
{
129+
list:[
130+
{
131+
id:'info',
132+
options:()=>({
133+
uri:'http://127.0.0.1/info.json',
134+
method:'GET',
135+
}),
136+
willFetch(options){
137+
return{
138+
...options,
139+
headers:{
140+
...options.headers,
141+
testHeader:'testHeaderValue',
142+
},
143+
};
144+
},
145+
},
146+
],
147+
},
148+
{
149+
state:state,
150+
setState(newState){
151+
Object.assign(state,newState);
152+
},
153+
forceUpdate:noop,
154+
}
155+
);
156+
constres=awaitengine.dataSourceMap.info.load();
157+
expect(res).eq(1);
158+
159+
expect(mockedFetch).toHaveBeenCalledWith('http://127.0.0.1/info.json',{
160+
credentials:'include',
161+
headers:{
162+
Accept:'application/json',
163+
testHeader:'testHeaderValue',
164+
},
165+
method:'GET',
166+
});
167+
});
168+
});
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import{fetchRequest}from'../src';
2+
3+
describe('fetch request',()=>{
4+
constmockedFetch=vi.fn();
5+
6+
beforeEach(()=>{
7+
vi.stubGlobal('fetch',mockedFetch);
8+
});
9+
10+
afterEach(()=>{
11+
vi.unstubAllEnvs();
12+
});
13+
14+
test('fetch success',async()=>{
15+
mockedFetch.mockResolvedValueOnce({
16+
json:()=>Promise.resolve(1),
17+
status:200,
18+
statusText:'OK',
19+
});
20+
constres=awaitfetchRequest({
21+
uri:'https://127.0.0.1/info.json',
22+
method:'GET',
23+
});
24+
expect(res.data).eq(1);
25+
expect(res.code).eq(200);
26+
});
27+
28+
test('fetch failure',async()=>{
29+
mockedFetch.mockResolvedValueOnce({
30+
json:()=>Promise.resolve(1),
31+
status:500,
32+
statusText:'Server internal error',
33+
});
34+
35+
awaitexpect(()=>
36+
fetchRequest({
37+
uri:'https://127.0.0.1/info.json',
38+
method:'GET',
39+
})
40+
).rejects.toThrowError('Server internal error');
41+
});
42+
43+
test('with params',async()=>{
44+
mockedFetch.mockResolvedValueOnce({
45+
json:()=>Promise.resolve(1),
46+
status:200,
47+
statusText:'OK',
48+
});
49+
constres=awaitfetchRequest({
50+
uri:'https://127.0.0.1/info.json',
51+
method:'GET',
52+
params:{a:5,b:null,c:{d:5}},
53+
});
54+
expect(res.data).eq(1);
55+
expect(res.code).eq(200);
56+
expect(mockedFetch).toMatchSnapshot();
57+
});
58+
59+
test('responseType',async()=>{
60+
mockedFetch.mockResolvedValueOnce({
61+
blob:()=>Promise.resolve(newBlob()),
62+
status:200,
63+
statusText:'OK',
64+
});
65+
constres=awaitfetchRequest({
66+
uri:'https://127.0.0.1/info.json',
67+
method:'POST',
68+
headers:{
69+
'content-type':'json',
70+
},
71+
params:{
72+
a:5,
73+
},
74+
responseType:'blob',
75+
});
76+
expect(res.code).eq(200);
77+
expect(res.data).toBeInstanceOf(Blob);
78+
expect(mockedFetch).toMatchSnapshot();
79+
});
80+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp