Skip to content

Commit af38702

Browse files
hiroheadamsol
andauthored
Add custom optionMergeStrategies to support mixins (#2)
* Add arrayValueOptionMergeStrategy function * Add test case of options merge * Update jest.config.js * Code cleanup and test improvements Co-authored-by: Adam Sołtysik <adam27.sol@gmail.com>
1 parent da7ac69 commit af38702

File tree

7 files changed

+42
-1
lines changed

7 files changed

+42
-1
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
module.exports = {
33
moduleFileExtensions: ['js', 'vue'],
44
testEnvironment: 'jsdom',
5-
testMatch: ['**/test/**/*.js'],
5+
testMatch: ['**/test/**/*.test.js'],
66
transform: {
77
'^.+\\.js$': 'babel-jest',
88
'^.+\\.vue$': 'vue-jest',

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,10 @@ export default {
3535
}
3636
},
3737
});
38+
39+
// Custom optionMergeStrategies to support mixins.
40+
for (const name of ['provideFields', 'injectFields', 'provideMethods', 'injectMethods']) {
41+
Vue.config.optionMergeStrategies[name] = (a, b) => a && b ? a.concat(b) : a || b;
42+
}
3843
},
3944
};

test/components/GrandChild.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
</template>
55

66
<script>
7+
import Injector from '../mixins/injector';
8+
79
export default {
10+
mixins: [Injector],
11+
812
injectFields: ['watch1', 'field2'],
913
injectMethods: ['negate3'],
1014

test/components/Main.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
</template>
55

66
<script>
7+
import Provider from '../mixins/provider';
78
import Child from './Child';
89
910
export default {
1011
components: { Child },
12+
mixins: [Provider],
1113
1214
provideFields: ['field1', 'field2', 'negated3'],
1315
provideMethods: ['increase2', 'negate3'],

test/index.js renamed to test/index.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,14 @@ test('bare provide/inject still works', () => {
6969
const wrapper = init();
7070
expect(wrapper.findComponent(GrandChild).vm.field4).toBe(1);
7171
});
72+
73+
test('new options work with mixins', async () => {
74+
const wrapper = init();
75+
expect(wrapper.findComponent(GrandChild).vm.field5).toBe(0);
76+
expect(wrapper.findComponent(Child).vm.field5).toBeUndefined();
77+
expect(wrapper.findComponent(Child).vm.increase5).toBeUndefined();
78+
79+
await wrapper.findComponent(GrandChild).vm.increase5();
80+
expect(wrapper.findComponent(Main).vm.field5).toBe(1);
81+
expect(wrapper.findComponent(GrandChild).vm.field5).toBe(1);
82+
});

test/mixins/injector.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
export default {
3+
injectFields: ['field5'],
4+
injectMethods: ['increase5'],
5+
};

test/mixins/provider.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
export default {
3+
provideFields: ['field5'],
4+
provideMethods: ['increase5'],
5+
6+
data: () => ({
7+
field5: 0,
8+
}),
9+
methods: {
10+
increase5(n = 1) {
11+
this.field5 += n;
12+
},
13+
},
14+
};

0 commit comments

Comments
 (0)