{{ count.value }}
22 |{{ NAME }}
23 |{{ app.text }}
26 | 27 | 28 | -------------------------------------------------------------------------------- /tests/kebab-case-props-child.vue: -------------------------------------------------------------------------------- 1 | 8 | 24 | 25 |{{ app.NameOne }}
26 |{{ app.nameAndAge }}
27 |{{ app.sSSS }}
28 | 29 | -------------------------------------------------------------------------------- /tests/use.spec.ts: -------------------------------------------------------------------------------- 1 | import { assert, test } from 'vitest'; 2 | import { mount } from '@vue/test-utils'; 3 | 4 | import Use from './use.vue'; 5 | 6 | test('Use', async () => { 7 | const wrapper = mount(Use); 8 | assert.equal(wrapper.find('.text').text(), '0'); 9 | assert.equal(wrapper.find('.text-eq').text(), 'true'); 10 | 11 | await wrapper.find('button').trigger('click'); 12 | 13 | assert.equal(wrapper.find('.text').text(), '1'); 14 | assert.equal(wrapper.find('.text-eq').text(), 'true'); 15 | 16 | wrapper.vm.addValue(); 17 | await wrapper.vm.$nextTick(); 18 | 19 | assert.equal(wrapper.find('.text').text(), '2'); 20 | assert.equal(wrapper.find('.text-eq').text(), 'true'); 21 | }); 22 | -------------------------------------------------------------------------------- /tests/extend-options.vue: -------------------------------------------------------------------------------- 1 | 30 | 31 |{{ value }}
32 | 33 | -------------------------------------------------------------------------------- /tests/watch-computed.spec.ts: -------------------------------------------------------------------------------- 1 | import { assert, test } from 'vitest'; 2 | import { mount } from '@vue/test-utils'; 3 | 4 | import WatchComputed from './watch-computed.vue'; 5 | 6 | test('Watch', async () => { 7 | const wrapper = mount(WatchComputed); 8 | assert.equal(wrapper.find('.value').text(), '0'); 9 | 10 | await wrapper.find('button').trigger('click'); 11 | 12 | assert.equal(wrapper.find('.value').text(), '1'); 13 | 14 | wrapper.vm.count.setValue(99); 15 | await wrapper.vm.$nextTick(); 16 | assert.equal(wrapper.find('.value').text(), '99'); 17 | 18 | wrapper.vm.count.setValue(110); 19 | await wrapper.vm.$nextTick(); 20 | assert.equal(wrapper.find('.value').text(), '100'); 21 | }); 22 | -------------------------------------------------------------------------------- /tests/demo.vue: -------------------------------------------------------------------------------- 1 | 24 | 25 |{{ text }}
27 | 28 |{{ count.initBtn }}
28 |{{ count.text }}
29 | 30 | -------------------------------------------------------------------------------- /tests/watch-effect.spec.ts: -------------------------------------------------------------------------------- 1 | import { assert, test } from 'vitest'; 2 | import { mount } from '@vue/test-utils'; 3 | 4 | import WatchEffect from './watch-effect.vue'; 5 | 6 | test('Watch effect', async () => { 7 | const wrapper = mount(WatchEffect); 8 | 9 | assert.equal(wrapper.find('.value').text(), '0'); 10 | assert.deepEqual(wrapper.vm.count.hooks, [ 11 | 'watchPreEffect', 12 | 'watchSyncEffect', 13 | 'beforeMount', 14 | 'watchPostEffect', 15 | ]); 16 | 17 | await wrapper.find('button.add').trigger('click'); 18 | 19 | assert.equal(wrapper.find('.value').text(), '1'); 20 | assert.deepEqual(wrapper.vm.count.hooks, [ 21 | 'watchSyncEffect', 22 | 'watchPreEffect', 23 | 'beforeUpdate', 24 | 'watchPostEffect', 25 | 'updated', 26 | ]); 27 | }); 28 | -------------------------------------------------------------------------------- /tests/register-hook.spec.ts: -------------------------------------------------------------------------------- 1 | import { assert, test } from 'vitest'; 2 | import { 3 | Setup, 4 | Define, 5 | PassOnTo, 6 | getCurrentHookContext, 7 | } from 'vue-class-setup'; 8 | 9 | function myFunc() { 10 | const { target, name } = getCurrentHookContext(); 11 | target[name](); 12 | } 13 | 14 | test('Register hook', () => { 15 | @Setup 16 | class Count extends Define { 17 | public value = 100; 18 | @PassOnTo(myFunc) 19 | public add() { 20 | this.value++; 21 | } 22 | } 23 | 24 | const count = new Count(); 25 | const { add } = count; 26 | add(); 27 | assert.equal(count.value, 102); 28 | }); 29 | 30 | test('error', () => { 31 | assert.Throw(() => { 32 | return getCurrentHookContext(); 33 | }, 'Can only be obtained in hook functions'); 34 | }); 35 | -------------------------------------------------------------------------------- /tests/quick-start.spec.ts: -------------------------------------------------------------------------------- 1 | import { assert, test } from 'vitest'; 2 | import { mount } from '@vue/test-utils'; 3 | 4 | import QuickStart from './quick-start.vue'; 5 | 6 | test('Base', async () => { 7 | const wrapper = mount(QuickStart); 8 | assert.equal(wrapper.find('p').text(), '0'); 9 | assert.equal(wrapper.find('input').element.value, '0'); 10 | await wrapper.vm.$nextTick(); 11 | assert.equal(wrapper.find('p').text(), '1'); 12 | assert.equal(wrapper.find('input').element.value, '1'); 13 | 14 | await wrapper.find('input').setValue('100'); 15 | assert.equal(wrapper.find('p').text(), '100'); 16 | assert.equal(wrapper.find('input').element.value, '100'); 17 | 18 | await wrapper.find('input').setValue(''); 19 | assert.equal(wrapper.find('p').text(), '0'); 20 | assert.equal(wrapper.find('input').element.value, '0'); 21 | }); 22 | -------------------------------------------------------------------------------- /tests/use.vue: -------------------------------------------------------------------------------- 1 | 22 | 29 | 30 |{{ text }}
32 |{{ app.text === text }}
33 | 34 |{{ count.value }}
36 | 37 | 38 | -------------------------------------------------------------------------------- /tests/watch.vue: -------------------------------------------------------------------------------- 1 | 27 | 30 | 31 |{{ app.value }}
32 |{{ app.immediateValue }}
33 | 34 | 35 | -------------------------------------------------------------------------------- /.github/workflows/node.js.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 3 | 4 | name: CI 5 | 6 | on: 7 | push: 8 | branches: ['main'] 9 | pull_request: 10 | branches: ['main'] 11 | 12 | jobs: 13 | build: 14 | runs-on: ubuntu-latest 15 | 16 | strategy: 17 | matrix: 18 | node-version: [16.x] 19 | # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ 20 | 21 | steps: 22 | - uses: actions/checkout@v3 23 | - name: Use Node.js ${{ matrix.node-version }} 24 | uses: actions/setup-node@v3 25 | # with: 26 | # node-version: ${{ matrix.node-version }} 27 | # cache: 'npm' 28 | - run: yarn test:all 29 | -------------------------------------------------------------------------------- /examples/vite-vue3/src/style.css: -------------------------------------------------------------------------------- 1 | :root { 2 | font-family: Inter, Avenir, Helvetica, Arial, sans-serif; 3 | font-size: 16px; 4 | line-height: 24px; 5 | font-weight: 400; 6 | 7 | color-scheme: light dark; 8 | color: rgba(255, 255, 255, 0.87); 9 | background-color: #242424; 10 | 11 | font-synthesis: none; 12 | text-rendering: optimizeLegibility; 13 | -webkit-font-smoothing: antialiased; 14 | -moz-osx-font-smoothing: grayscale; 15 | -webkit-text-size-adjust: 100%; 16 | } 17 | 18 | a { 19 | font-weight: 500; 20 | color: #646cff; 21 | text-decoration: inherit; 22 | } 23 | a:hover { 24 | color: #535bf2; 25 | } 26 | 27 | body { 28 | margin: 0; 29 | } 30 | 31 | h1 { 32 | font-size: 3.2em; 33 | line-height: 1.1; 34 | } 35 | @media (prefers-color-scheme: light) { 36 | :root { 37 | color: #213547; 38 | background-color: #ffffff; 39 | } 40 | a:hover { 41 | color: #747bff; 42 | } 43 | button { 44 | background-color: #f9f9f9; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/setup-reference.ts: -------------------------------------------------------------------------------- 1 | let count = 0; 2 | let isBind = false; 3 | 4 | export function addCount() { 5 | // 如果还是处于绑定状态,说明上一次解绑的过程中程序执行报错了,需要重置 6 | if (isBind) { 7 | isBind = false; 8 | count = 1; 9 | } else { 10 | count++; 11 | } 12 | } 13 | 14 | const weakMap = new WeakMap