Skip to content

Commit 22bdad2

Browse files
committed
新增pinia测试例
1 parent 40b10c6 commit 22bdad2

File tree

6 files changed

+124
-3
lines changed

6 files changed

+124
-3
lines changed

main.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ app.$mount()
2222
// #ifdef VUE3
2323
import {
2424
createSSRApp
25-
} from 'vue'
25+
} from 'vue'
26+
import * as Pinia from 'pinia';
27+
import Vuex from "vuex";
2628
export function createApp() {
2729
const app = createSSRApp(App)
28-
app.use(store)
30+
app.use(store)
31+
app.use(Pinia.createPinia());
2932
app.config.globalProperties.$adpid = "1111111111"
3033
app.config.globalProperties.$backgroundAudioData = {
3134
playing: false,
3235
playTime: 0,
3336
formatedPlayTime: '00:00:00'
3437
}
3538
return {
36-
app
39+
app,
40+
Vuex, // 如果 nvue 使用 vuex 的各种map工具方法时,必须 return Vuex
41+
Pinia // 此处必须将 Pinia 返回
3742
}
3843
}
3944
// #endif

pages.json

+11
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,17 @@
13361336
"navigationBarTitleText": "GlobalData和vuex"
13371337
}
13381338
}
1339+
// #ifdef VUE3
1340+
,
1341+
{
1342+
"path" : "pinia/pinia",
1343+
"style" :
1344+
{
1345+
"navigationBarTitleText" : "pinia"
1346+
}
1347+
}
1348+
// #endif
1349+
13391350
]
13401351
}
13411352
],

pages/tabBar/template/template.nvue

+6
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@
122122
name: 'GlobalData和vuex',
123123
url: 'global'
124124
},
125+
// #ifdef VUE3
126+
{
127+
name: 'pinia',
128+
url: 'pinia'
129+
},
130+
// #endif
125131
{
126132
name: 'renderjs',
127133
url: 'renderjs'

pages/template/pinia/pinia.test.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
describe('test pinia', () => {
2+
let page,containsVite;
3+
containsVite = process.env.UNI_CLI_PATH.includes('uniapp-cli-vite')
4+
if (!containsVite) {
5+
// Vue 2 项目暂不支持 Pinia
6+
it('is vue2', async () => {
7+
expect(1).toBe(1)
8+
})
9+
return
10+
}
11+
beforeAll(async () => {
12+
page = await program.reLaunch('/pages/template/pinia/pinia')
13+
await page.waitFor('view');
14+
});
15+
it('check page data', async () => {
16+
const count = await page.$('.count');
17+
expect(await count.text()).toEqual('当前计数:0');
18+
const doubleCount = await page.$('.doubleCount');
19+
expect(await doubleCount.text()).toEqual('计数翻倍:0');
20+
});
21+
it('store incrementCounter', async () => {
22+
await page.callMethod('incrementCounter')
23+
const count = await page.$('.count');
24+
expect(await count.text()).toEqual('当前计数:1');
25+
const doubleCount = await page.$('.doubleCount');
26+
expect(await doubleCount.text()).toEqual('计数翻倍:2');
27+
});
28+
it('store incrementPatchCounter', async () => {
29+
await page.callMethod('incrementPatchCounter')
30+
const count = await page.$('.count');
31+
expect(await count.text()).toEqual('当前计数:2');
32+
const doubleCount = await page.$('.doubleCount');
33+
expect(await doubleCount.text()).toEqual('计数翻倍:4');
34+
});
35+
it('store actions', async () => {
36+
const increment = await page.$('.increment');
37+
await increment.tap()
38+
const countIn = await page.$('.count');
39+
expect(await countIn.text()).toEqual('当前计数:3');
40+
const doubleCountIn = await page.$('.doubleCount');
41+
expect(await doubleCountIn.text()).toEqual('计数翻倍:6');
42+
43+
const decrement = await page.$('.decrement');
44+
await decrement.tap()
45+
const countDe = await page.$('.count');
46+
expect(await countDe.text()).toEqual('当前计数:2');
47+
const doubleCountDe = await page.$('.doubleCount');
48+
expect(await doubleCountDe.text()).toEqual('计数翻倍:4');
49+
});
50+
});

pages/template/pinia/pinia.vue

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<template>
2+
<view class="uni-product">
3+
<page-head title="Pinia"></page-head>
4+
<text class="count">当前计数:{{ counter.count }}</text>
5+
<text class="doubleCount" style="margin: 20rpx 0;">计数翻倍:{{ counter.doubleCount }}</text>
6+
<button @click="incrementCounter">增加计数</button>
7+
<button @click="incrementPatchCounter">使用$patch方法增加计数</button>
8+
<button class="increment" @click="counter.increment">通过actions中的increment增加计数</button>
9+
<button class="decrement" @click="counter.decrement">通过actions中的decrement减少计数</button>
10+
</view>
11+
</template>
12+
13+
<script>
14+
import { useCounterStore } from '@/store/counter'
15+
export default {
16+
setup() {
17+
const counter = useCounterStore()
18+
function incrementCounter() {
19+
counter.count++
20+
}
21+
function incrementPatchCounter() {
22+
// 或者使用 $patch 方法
23+
counter.$patch({ count: counter.count + 1 })
24+
}
25+
return {
26+
counter,
27+
incrementCounter,
28+
incrementPatchCounter
29+
}
30+
}
31+
}
32+
</script>

store/counter.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineStore } from 'pinia'
2+
export const useCounterStore = defineStore('counter', {
3+
state: () => ({
4+
count: 0
5+
}),
6+
getters: {
7+
doubleCount: (state) => state.count * 2
8+
},
9+
actions: {
10+
increment() {
11+
this.count++
12+
},
13+
decrement() {
14+
this.count--
15+
}
16+
}
17+
})

0 commit comments

Comments
 (0)