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

Commit981eed7

Browse files
gitstart_botjschwartzentruber
gitstart_bot
authored andcommitted
Setup test and update test to support vue 3
1 parent138c137 commit981eed7

File tree

3 files changed

+131
-86
lines changed

3 files changed

+131
-86
lines changed
Lines changed: 92 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
1-
import{nextTick}from"vue";
2-
import{createLocalVue,createWrapper}from"@vue/test-utils";
3-
importVueRouterfrom"vue-router";
41
import{render}from"@testing-library/vue";
5-
importStatsfrom"../src/components/CrashStats.vue";
6-
import{crashStats,listBuckets}from"../src/api.js";
7-
import{emptyCrashStats,crashStatsData,buckets}from"./fixtures.js";
2+
import{mount}from"@vue/test-utils";
83
import"lodash/throttle";
4+
import{nextTick}from"vue";
5+
import{crashStats,listBuckets}from"../src/api.js";
6+
importStatsfrom"../src/components/CrashStats.vue";
7+
import{buckets,crashStatsData,emptyCrashStats}from"./fixtures.js";
98

109
// This line will mock all calls to functions in ../src/api.js
1110
jest.mock("../src/api.js");
11+
1212
// Mocking calls to lodash._throttle during tests
1313
jest.mock("lodash/throttle",()=>jest.fn((fn)=>fn));
1414

1515
afterEach(jest.resetAllMocks);
1616

1717
test("empty stats doesn't break",async()=>{
18-
constlocalVue=createLocalVue();
19-
localVue.use(VueRouter);
20-
constrouter=newVueRouter();
21-
2218
crashStats.mockResolvedValue(emptyCrashStats);
23-
awaitrender(Stats,{
24-
localVue,
25-
router,
19+
20+
const{ container}=render(Stats,{
2621
props:{
2722
restricted:false,
2823
providers:[],
2924
activityRange:14,
3025
},
3126
});
27+
3228
awaitnextTick();
3329

3430
expect(crashStats).toHaveBeenCalledTimes(1);
@@ -37,95 +33,127 @@ test("empty stats doesn't break", async () => {
3733
});
3834
expect(listBuckets).toHaveBeenCalledTimes(0);
3935

40-
awaitnextTick();
4136
// Assert no signature is displayed in the table
42-
expect(document.querySelector("tbody tr")).toBeNull();
37+
expect(container.querySelector("tbody tr")).toBeNull();
4338
});
4439

4540
test("stats are shown",async()=>{
46-
constlocalVue=createLocalVue();
47-
localVue.use(VueRouter);
48-
constrouter=newVueRouter();
49-
5041
crashStats.mockResolvedValue(crashStatsData);
5142
listBuckets.mockResolvedValue(buckets);
52-
const{ getByText}=awaitrender(Stats,{
53-
localVue,
54-
router,
43+
44+
const{ getByText, container}=render(Stats,{
5545
props:{
5646
restricted:false,
5747
providers:[],
5848
activityRange:14,
5949
},
6050
});
51+
6152
awaitnextTick();
6253

6354
expect(crashStats).toHaveBeenCalledTimes(1);
6455
expect(crashStats).toHaveBeenCalledWith({
6556
ignore_toolfilter:"0",
6657
});
6758
expect(listBuckets).toHaveBeenCalledTimes(1);
68-
awaitnextTick();
69-
awaitnextTick();
59+
7060
awaitnextTick();
7161

72-
// Assert two signatures are displayed in the table
73-
expect(document.querySelectorAll("tbody tr").length).toBe(2);
62+
expect(container.querySelectorAll("tbody tr").length).toBe(2);
7463
getByText("A short description for bucket 1");
7564
constbuttonLink=getByText("1630739");
76-
expect(buttonLink).toHaveProperty("href",buckets[0].bug_urltemplate);
77-
expect(buttonLink).toHaveProperty("target","_blank");
65+
expect(buttonLink.getAttribute("href")).toBe(buckets[0].bug_urltemplate);
66+
expect(buttonLink.getAttribute("target")).toBe("_blank");
7867
getByText("A short description for bucket 2");
7968
getByText("Assign an existing bug");
8069
});
8170

8271
test("stats use hash params",async()=>{
8372
crashStats.mockResolvedValue(crashStatsData);
8473
listBuckets.mockResolvedValue(buckets);
85-
constlocalVue=createLocalVue();
86-
const$route={path:"/stats",hash:"#sort=id&alltools=1"};
87-
const$router=[];
88-
awaitrender(Stats,{
89-
localVue,
90-
mocks:{
91-
$route,
92-
$router,
93-
},
74+
75+
const{ container}=render(Stats,{
9476
props:{
9577
restricted:false,
9678
providers:[],
9779
activityRange:14,
9880
},
81+
global:{
82+
mocks:{
83+
$route:{path:"/stats",hash:"#sort=id&alltools=1"},
84+
$router:[],
85+
},
86+
},
9987
});
88+
10089
awaitnextTick();
10190

10291
expect(crashStats).toHaveBeenCalledTimes(1);
10392
expect(crashStats).toHaveBeenCalledWith({
10493
ignore_toolfilter:"1",
10594
});
10695
expect(listBuckets).toHaveBeenCalledTimes(1);
107-
awaitnextTick();
108-
awaitnextTick();
96+
10997
awaitnextTick();
11098

111-
expect(document.querySelectorAll("tbody tr").length).toBe(2);
99+
expect(container.querySelectorAll("tbody tr").length).toBe(2);
112100
});
113101

114102
test("stats are sortable",async()=>{
115103
crashStats.mockResolvedValue(crashStatsData);
116104
listBuckets.mockResolvedValue(buckets);
117-
constlocalVue=createLocalVue();
118-
localVue.use(VueRouter);
119-
constrouter=newVueRouter();
120-
awaitrender(Stats,{
121-
localVue,
122-
router,
105+
// Create a reactive route object that can be updated
106+
constroute={
107+
hash:"",
108+
};
109+
110+
// Create a mock router with push method
111+
constrouter={
112+
push:jest.fn((value)=>{
113+
constnewHash=value.hash.slice(1);
114+
constcurrentHash=route.hash.slice(1);
115+
116+
// Get the new sort parameter (e.g., "id" or "-id")
117+
constnewSortParam=newHash.split("=")[1];
118+
constnewSortKey=newSortParam.replace(/^-/,"");
119+
120+
constcurrentParams=currentHash ?currentHash.split(",") :[];
121+
122+
constotherParams=currentParams
123+
.filter((param)=>{
124+
if(!param.startsWith("sort="))returntrue;
125+
constexistingSortKey=param.split("=")[1].replace(/^-/,"");
126+
returnexistingSortKey!==newSortKey;
127+
})
128+
.map((param)=>{
129+
if(param.startsWith("sort=")){
130+
returnparam.slice(5);
131+
}
132+
returnparam;
133+
});
134+
135+
// Combine the new sort with existing parameters
136+
route.hash="#"+[newHash, ...otherParams].filter(Boolean).join(",");
137+
}),
138+
};
139+
140+
constwrapper=mount(Stats,{
123141
props:{
124142
restricted:false,
125143
providers:[],
126144
activityRange:14,
127145
},
146+
global:{
147+
stubs:{
148+
RouterLink:true,
149+
},
150+
mocks:{
151+
$route:route,
152+
$router:router,
153+
},
154+
},
128155
});
156+
129157
awaitnextTick();
130158

131159
expect(crashStats).toHaveBeenCalledTimes(1);
@@ -134,38 +162,36 @@ test("stats are sortable", async () => {
134162
awaitnextTick();
135163
awaitnextTick();
136164

137-
letrows=document.querySelectorAll("tbody tr");
165+
letrows=wrapper.findAll("tbody tr");
138166
expect(rows.length).toBe(2);
139167
// sorted by daily count by default
140-
expect(rows[0].querySelector("td").textContent).toBe("2");
141-
expect(rows[1].querySelector("td").textContent).toBe("1");
142-
expect(router.currentRoute.hash).toBe("");
168+
expect(rows[0].find("td").text()).toBe("2");
169+
expect(rows[1].find("td").text()).toBe("1");
170+
expect(route.hash).toBe("");
143171

144-
awaitcreateWrapper(document.querySelector("thead th")).trigger("click");
172+
awaitwrapper.find("theadtrth").trigger("click");
145173
awaitnextTick();
146174

147-
rows=document.querySelectorAll("tbody tr");
175+
rows=wrapper.findAll("tbody tr");
148176
expect(rows.length).toBe(2);
149177
// sorted by id now
150-
expect(rows[0].querySelector("td").textContent).toBe("2");
151-
expect(rows[1].querySelector("td").textContent).toBe("1");
152-
expect(router.currentRoute.hash).toBe("#sort=-id");
178+
expect(rows[0].find("td").text()).toBe("2");
179+
expect(rows[1].find("td").text()).toBe("1");
180+
expect(router.push).toHaveBeenCalledTimes(1);
181+
expect(route.hash).toBe("#sort=-id");
153182

154-
awaitcreateWrapper(document.querySelector("thead th")).trigger("click");
183+
awaitwrapper.find("thead th").trigger("click");
155184
awaitnextTick();
156185

157-
rows=document.querySelectorAll("tbody tr");
186+
rows=wrapper.findAll("tbody tr");
158187
expect(rows.length).toBe(2);
159188
// sorted by -id now
160-
expect(rows[0].querySelector("td").textContent).toBe("1");
161-
expect(rows[1].querySelector("td").textContent).toBe("2");
162-
expect(router.currentRoute.hash).toBe("#sort=id");
163-
164-
awaitcreateWrapper(document.querySelector("thead th + th")).trigger(
165-
"click",
166-
{ctrlKey:true},
167-
);
189+
expect(rows[0].find("td").text()).toBe("1");
190+
expect(rows[1].find("td").text()).toBe("2");
191+
expect(route.hash).toBe("#sort=id");
192+
193+
awaitwrapper.find("thead th + th").trigger("click",{key:"Control"});
168194
awaitnextTick();
169195

170-
expect(router.currentRoute.hash).toBe("#sort=-shortDescription,id");
196+
expect(route.hash).toBe("#sort=-shortDescription,id");
171197
});

‎server/frontend/tests/setup.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import{config}from"@vue/test-utils";
2+
3+
// Setup global mocks
4+
config.global.mocks={
5+
$route:{
6+
hash:"",
7+
query:{},
8+
params:{},
9+
path:"/",
10+
name:null,
11+
},
12+
};
Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
import{nextTick}from"vue";
2-
import{createLocalVue}from"@vue/test-utils";
3-
importVueRouterfrom"vue-router";
41
import{render}from"@testing-library/vue";
5-
importListfrom"../src/components/Signatures/List.vue";
6-
import{listBuckets}from"../src/api.js";
7-
import{emptyBuckets,buckets}from"./fixtures.js";
82
import"lodash/throttle";
9-
10-
constlocalVue=createLocalVue();
11-
localVue.use(VueRouter);
12-
constrouter=newVueRouter();
3+
import{nextTick}from"vue";
4+
import{listBuckets}from"../src/api.js";
5+
importListfrom"../src/components/Signatures/List.vue";
6+
import{buckets,emptyBuckets}from"./fixtures.js";
137

148
// This line will mock all calls to functions in ../src/api.js
159
jest.mock("../src/api.js");
10+
1611
// Mocking calls to lodash._throttle during tests
1712
jest.mock("lodash/throttle",()=>jest.fn((fn)=>fn));
1813

@@ -25,15 +20,20 @@ const defaultQueryStr = `{
2520

2621
test("signature list has no buckets",async()=>{
2722
listBuckets.mockResolvedValue(emptyBuckets);
28-
awaitrender(List,{
29-
localVue,
30-
router,
23+
24+
const{ container}=awaitrender(List,{
3125
props:{
3226
watchUrl:"/crashmanager/signatures/watch/",
3327
providers:[],
3428
activityRange:14,
3529
},
30+
global:{
31+
mocks:{
32+
$router:[],
33+
},
34+
},
3635
});
36+
3737
awaitnextTick();
3838

3939
expect(listBuckets).toHaveBeenCalledTimes(1);
@@ -44,21 +44,27 @@ test("signature list has no buckets", async () => {
4444
});
4545

4646
awaitnextTick();
47+
4748
// Assert no signature is displayed in the table
48-
expect(document.querySelector("tbody tr")).toBeNull();
49+
expect(container.querySelector("tbody tr")).toBeNull();
4950
});
5051

5152
test("signature list has two buckets",async()=>{
5253
listBuckets.mockResolvedValue(buckets);
53-
const{ getByText}=awaitrender(List,{
54-
localVue,
55-
router,
54+
55+
const{ getByText, container}=awaitrender(List,{
5656
props:{
5757
watchUrl:"/crashmanager/signatures/watch/",
5858
providers:[],
5959
activityRange:14,
6060
},
61+
global:{
62+
mocks:{
63+
$router:[],
64+
},
65+
},
6166
});
67+
6268
awaitnextTick();
6369

6470
expect(listBuckets).toHaveBeenCalledTimes(1);
@@ -69,12 +75,13 @@ test("signature list has two buckets", async () => {
6975
});
7076

7177
awaitnextTick();
78+
7279
// Assert two signatures (one assigned to a bug, the other not) are displayed in the table
73-
expect(document.querySelectorAll("tbody tr").length).toBe(2);
80+
expect(container.querySelectorAll("tbody tr")).toHaveLength(2);
7481
getByText("A short description for bucket 1");
7582
constbuttonLink=getByText("1630739");
76-
expect(buttonLink).toHaveProperty("href",buckets[0].bug_urltemplate);
77-
expect(buttonLink).toHaveProperty("target","_blank");
83+
expect(buttonLink.getAttribute("href")).toBe(buckets[0].bug_urltemplate);
84+
expect(buttonLink.getAttribute("target")).toBe("_blank");
7885
getByText("A short description for bucket 2");
7986
getByText("Assign an existing bug");
8087
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp