How to use provide method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

moduleProviderPlugIns.js

Source: moduleProviderPlugIns.js Github

copy
1newTestSet("Module provider plug-ins");
2
3asyncTest("Overriden load: is called when calling module.provide, with the correct identifier", function () {
4    var identifierLoadWasCalledWith = null;
5
6    var originalModuleLoad = module.constructor.prototype.load;
7    module.constructor.prototype.load = function (moduleIdentifier, onModuleLoaded) {
8        identifierLoadWasCalledWith = moduleIdentifier;
9
10        originalModuleLoad(moduleIdentifier, onModuleLoaded);
11    };
12
13    module.declare([], function () {
14        module.provide(["demos/math"], function () {
15            strictEqual(identifierLoadWasCalledWith, "demos/math", "The overriden version of module.load was called with the same module identifier as was passed to the un-overriden module.provide.");
16
17            module.constructor.prototype.load = originalModuleLoad;
18            start();
19        });
20    });
21});
22
23test("Overriden provide: is called to provide the unmemoized dependencies when declaring the main module", function () {
24    var idsOfModulesProvideIsCalledOn = [];
25    var dependenciesProvideWasCalledWith = [];
26
27    var originalModuleProvide = module.constructor.prototype.provide;
28    module.constructor.prototype.provide = function (dependencies, onAllProvided) {
29        idsOfModulesProvideIsCalledOn.push(this.id);
30        dependenciesProvideWasCalledWith = dependenciesProvideWasCalledWith.concat(dependencies);
31        onAllProvided();
32    };
33
34    require.memoize("dependency/1", [], function () { });
35
36    module.declare(["dependency/1", "dependency/2"], function () {
37        ok(idsOfModulesProvideIsCalledOn.indexOf("") !== -1, "The overriden version of module.provide was called with this.id set correctly");
38        ok(dependenciesProvideWasCalledWith.indexOf("dependency/2") !== -1, "The overriden version of module.provide was called for the un-memoized dependency");
39    });
40
41    module.constructor.prototype.provide = originalModuleProvide;
42});
43
44asyncTest("Overriden provide: is called to provide the un-memoized dependencies of a memoized dependency of a memoized dependency of the main module", function () {
45    var idsOfModulesProvideIsCalledOn = [];
46    var dependenciesProvideWasCalledWith = [];
47
48    var originalModuleProvide = module.constructor.prototype.provide;
49    module.constructor.prototype.provide = function (dependencies, onAllProvided) {
50        idsOfModulesProvideIsCalledOn.push(this.id);
51        dependenciesProvideWasCalledWith = dependenciesProvideWasCalledWith.concat(dependencies);
52
53        originalModuleProvide.call(this, dependencies, onAllProvided);
54    };
55
56    require.memoize("deeperDependency", ["demos/math"], function () { });
57    require.memoize("dependency", ["deeperDependency"], function () { });
58    require.memoize("memoized", ["dependency"], function () { });
59
60    module.declare(["memoized"], function () {
61        ok(idsOfModulesProvideIsCalledOn.indexOf("deeperDependency") !== -1, "The overriden version of module.provide was called with this.id set to that of the two-levels-deep dependency");
62        ok(dependenciesProvideWasCalledWith.indexOf("demos/math") !== -1, "The overriden version of module.provide was called for the un-memoized dependency");
63
64        module.constructor.prototype.provide = originalModuleProvide;
65        start();
66    });
67});
68
69asyncTest("Overriden provide: works even with circular dependencies", function () {
70    var idsOfModulesProvideIsCalledOn = [];
71    var dependenciesProvideWasCalledWith = [];
72
73    var originalModuleProvide = module.constructor.prototype.provide;
74    module.constructor.prototype.provide = function (dependencies, onAllProvided) {
75        idsOfModulesProvideIsCalledOn.push(this.id);
76        dependenciesProvideWasCalledWith = dependenciesProvideWasCalledWith.concat(dependencies);
77
78        originalModuleProvide.call(this, dependencies, onAllProvided);
79    };
80
81    module.declare(["demos/circular/circularA"], function () {
82        ok(idsOfModulesProvideIsCalledOn.indexOf("demos/circular/circularA") !== -1, "The overriden version of module.provide was called with this.id set to that of the module the main module depends on");
83        ok(dependenciesProvideWasCalledWith.indexOf("demos/circular/circularB") !== -1, "The overriden version of module.provide was called to provide the other module in the circular dependency chain");
84
85        module.constructor.prototype.provide = originalModuleProvide;
86        start();
87    });
88});
89
90asyncTest("Overriden provide: works even with circular dependencies where the plug-in provides a normally nonextant module", function () {
91    var idsOfModulesProvideIsCalledOn = [];
92    var dependenciesProvideWasCalledWith = [];
93
94    var originalModuleProvide = module.constructor.prototype.provide;
95    module.constructor.prototype.provide = function (dependencies, onAllProvided) {
96        idsOfModulesProvideIsCalledOn.push(this.id);
97        dependenciesProvideWasCalledWith = dependenciesProvideWasCalledWith.concat(dependencies);
98
99        if (dependencies.indexOf("asdf") !== -1) {
100            if (!require.isMemoized("asdf")) {
101                require.memoize("asdf", [], function () { });
102            }
103
104            dependencies = dependencies.splice(dependencies.indexOf("asdf"), 1);
105        }
106
107        originalModuleProvide.call(this, dependencies, onAllProvided);
108    };
109
110    module.declare(["demos/circular/circularAndNonextantA"], function () {
111        ok(idsOfModulesProvideIsCalledOn.indexOf("demos/circular/circularAndNonextantA") !== -1, "The overriden version of module.provide was called with this.id set to that of the module the main module depends on");
112        ok(dependenciesProvideWasCalledWith.indexOf("demos/circular/circularAndNonextantB") !== -1, "The overriden version of module.provide was called to provide the other module in the circular dependency chain");
113        ok(dependenciesProvideWasCalledWith.indexOf("asdf") !== -1, "The overriden version of module.provide was called to provide the module the un-memoized module outside the circular dependency chain");
114
115        module.constructor.prototype.provide = originalModuleProvide;
116        start();
117    });
118});
119
120asyncTest("Overriden provide: can provide a dependency to a dependency of a memoized module", function () {
121    var idsOfModulesProvideIsCalledOn = [];
122    var dependenciesProvideWasCalledWith = [];
123
124    var originalModuleProvide = module.constructor.prototype.provide;
125    module.constructor.prototype.provide = function (dependencies, onAllProvided) {
126        idsOfModulesProvideIsCalledOn.push(this.id);
127        dependenciesProvideWasCalledWith = dependenciesProvideWasCalledWith.concat(dependencies);
128
129        var desiredOperationsCompleted = 1;
130        var operationsCompleted = 0;
131        function completeOperation() {
132            if (++operationsCompleted === desiredOperationsCompleted) {
133                onAllProvided();
134            }
135        }
136
137        if (dependencies.indexOf("providedByPlugin") !== -1) {
138            if (!require.isMemoized("providedByPlugin")) {
139                ++desiredOperationsCompleted;
140                setTimeout(function () {
141                    require.memoize("providedByPlugin", [], function () { });
142                    completeOperation();
143                }, 0);
144            }
145
146            dependencies = dependencies.splice(dependencies.indexOf("asdf"), 1);
147        }
148
149        originalModuleProvide.call(this, dependencies, completeOperation);
150    };
151
152    require.memoize("memoized", ["providedByPlugin", "demos/math"], function () { });
153
154    module.declare(["memoized"], function () {
155        ok(idsOfModulesProvideIsCalledOn.indexOf("memoized") !== -1, "The overriden version of module.provide was called with this.id set to that of the memoized dependency");
156        ok(dependenciesProvideWasCalledWith.indexOf("providedByPlugin") !== -1, "The overriden version of module.provide was called to provide the module that was only available via plug-in provision");
157
158        module.constructor.prototype.provide = originalModuleProvide;
159        start();
160    });
161});
162
163test("Overriden declare: is called when declaring the main module", function () {
164    var dependenciesDeclareWasCalledWith = null;
165    var factoryFunctionDeclareWasCalledWith = null;
166
167    var originalModuleDeclare = module.constructor.prototype.declare;
168    module.constructor.prototype.declare = function (dependencies, moduleFactory) {
169        dependenciesDeclareWasCalledWith = dependencies;
170        factoryFunctionDeclareWasCalledWith = moduleFactory;
171    };
172
173    var dependencies = ["dependency/1", "dependency/2"];
174    var moduleFactory = function () { };
175    module.declare(dependencies, moduleFactory);
176
177    deepEqual(dependenciesDeclareWasCalledWith, dependencies, "The correct dependencies array was passed to the overriden module.declare");
178    strictEqual(factoryFunctionDeclareWasCalledWith, moduleFactory, "The correct factory function was passed to the overriden module.declare");
179
180    module.constructor.prototype.declare = originalModuleDeclare;
181});
182
183asyncModuleTest("Overriden declare: is called when using module.load", function (require, exports, module) {
184    var mathExports = {};
185
186    var originalModuleDeclare = module.constructor.prototype.declare;
187    module.constructor.prototype.declare = function (moduleFactory) {
188        // We know that demos/math does not return alternate exports and does not pass the optional dependency array,
189        // so we implement a stub based on that knowledge instead of a more general one.
190        moduleFactory(null, mathExports, null);
191    };
192
193    module.load("demos/math", function () {
194        strictEqual(typeof mathExports.add, "function", "Executing the factory function that was passed to the overriden module.declare returns an object with the expected exports");
195
196        module.constructor.prototype.declare = originalModuleDeclare;
197        start();
198    });
199});
200
Full Screen

provide.controller.maintain.js

Source: provide.controller.maintain.js Github

copy
1define(['apps/system3/production/production.controller',
2        'apps/system3/production/specialty/specialty.service',
3        'apps/system3/production/provide/provide.service'], function (app) {
4
5
6            app.controller('production.controller.provide.maintain', function ($scope, maintainParams, $uibModalInstance) {
7
8                $scope.windowProvide = maintainParams.provideInfo;
9
10                if (maintainParams.task) {
11                    $scope.taskID = maintainParams.task.ID;
12                }
13
14                $scope.receiveUsers = [];
15                $scope.volumeFiles = [];
16                $scope.currentVolume = {};
17
18                // 新建提资
19                $scope.newProvide = function (provide) {
20
21                    $scope.readOnly = false;
22
23                    $scope.provideInfo = {
24                        Engineering: provide.Engineering,
25                        EngineeringID: provide.Engineering.ID,
26                        SendSpecialtyID: provide.Specialty.SpecialtyID,
27                        ReceiveUserIDs: "",
28                        DocName: "",
29                        DocContent: "",
30                        AttachIDs: [],
31                        VolumeFiles: []
32                    };
33
34                    $scope.flowData = {
35                        EngineeringID: provide.Engineering.ID,
36                        SpecialtyID: provide.Specialty.SpecialtyID
37                    };
38
39                    // 新增加载专业的卷册文件
40                    $scope.loadVolumeFiles();
41                }
42
43                // 编辑提资
44                $scope.maintainProvide = function (provide, task) {
45
46                    $scope.readOnly = !$scope.taskID;
47
48                    $scope.provideInfo = provide;
49
50                    if (!$scope.readOnly) {
51
52                        if (provide.ReceiveUserIDs.length > 0) {
53
54                            var str = ',' + provide.ReceiveUserIDs + ',';
55
56                            angular.forEach($rootScope.user_item, function (user) {
57
58                                if (str.indexOf(',' + user.ID + ',') >= 0) {
59                                    $scope.receiveUsers.push({
60                                        ID: user.ID,
61                                        Name: user.Name,
62                                        Dept: user.Dept,
63                                        PhotoImg: user.PhotoImg
64                                    });
65                                }
66                            });
67                        }
68
69                        // 新增加载专业的卷册文件
70                        $scope.loadVolumeFiles();
71                    }
72                }
73
74                // 获取卷册文件
75                $scope.loadVolumeFiles = function () {
76                    console.log(111)
77                    //$scope.providePanel.block();
78                    specialtyService.getFiles($scope.provideInfo.EngineeringID, $scope.provideInfo.SendSpecialtyID).then(function (data) {
79                        $scope.volumeFiles = data;
80                        $scope.currentVolume = data[0];
81                        //$scope.providePanel.unblock();
82                    });
83                }
84
85                // 选择卷册
86                $scope.volChanged = function (v) {
87                    $scope.currentVolume = v;
88                }
89
90                // 全选卷册文件
91                $scope.selectAll = function (vol) {
92                    angular.forEach(vol.Files, function (f) {
93                        f.selected = vol.selected;
94                    });
95                }
96
97                // 获取选中卷册文件的数量
98                $scope.getSelectedFileCount = function (files) {
99                    var count = 0;
100                    angular.forEach(files, function (f) {
101                        if (f.selected) {
102                            count++;
103                        }
104                    });
105                    return count;
106                }
107
108                // 保存
109                $scope.save = function (flow) {
110
111                    $scope.providePanel.block();
112
113                    if ($scope.provideInfo.ID > 0) {
114                        // 编辑
115                        provideService.update($scope.provideInfo).then(function () {
116                            flow.callBack(function () {
117                                $scope.providePanel.unblock();
118
119                                if ($scope.afterSave) {
120                                    $scope.afterSave();
121                                } else {
122                                    $scope.loadSource();
123                                }
124                            });
125                        });
126                    } else {
127
128                        // 新增
129                        $scope.provideInfo.ApproveUser = flow.taskInfo.user;
130
131                        provideService.create($scope.provideInfo).then(function () {
132
133                            // 通知附件预览控件更新附件列表
134                            if ($scope.attachChangedCB) {
135                                $scope.attachChangedCB();
136                            }
137
138                            flow.callBack(function () {
139                                $scope.providePanel.unblock();
140
141                                if ($scope.afterSave) {
142                                    $scope.afterSave();
143                                } else {
144                                    $scope.loadSource();
145                                }
146                            });
147                        });
148                    }
149
150                    return true;
151                }
152
153
154                $scope.$watchCollection("receiveUsers", function (newval, oldval) {
155                    if (newval && $scope.provideInfo) {
156
157                        if (newval.length == 0) {
158                            $scope.provideInfo.ReceiveUserIDs = undefined;
159                        } else {
160                            var ids = [];
161                            angular.forEach(newval, function (user) {
162                                ids.push(user.ID);
163                            })
164                            $scope.provideInfo.ReceiveUserIDs = ids.join(',');
165                        }
166                    }
167                });
168
169                $scope.$watch("volumeFiles", function (newval, oldval) {
170                    if (newval && $scope.provideInfo) {
171                        $scope.provideInfo.VolumeFiles = [];
172                        angular.forEach(newval, function (vol) {
173                            angular.forEach(vol.Files, function (f) {
174                                if (f.selected) {
175                                    $scope.provideInfo.VolumeFiles.push(f.ID);
176                                }
177                            });
178                        });
179                    }
180                }, true);
181
182                $scope.$watch("currentProvide", function (newval, oldval) {
183                    if (newval) {
184                        if (newval.ID > 0) {
185                            $scope.maintainProvide(newval);
186                        } else {
187                            $scope.newProvide(newval);
188                        }
189                    }
190                });
191
192                $scope.$on("newProvide", function (e, data) {
193                    $scope.newProvide(data);
194                })
195
196                // 附件上传完成后回调
197                $scope.attachUploaded = function (id, attachChangedCB) {
198
199                    // 新建的模式下,先保存上传的附件ID,当信息被保存后,在保存该附件ID
200                    $scope.provideInfo.AttachIDs.push(id);
201
202                    $scope.attachChangedCB = attachChangedCB;
203                };
204
205                // 变更信息
206                if ($scope.windowProvide) {
207                    if ($scope.windowProvide.ID > 0) {
208                        $scope.maintainProvide($scope.windowProvide)
209                    } else {
210                        $scope.newProvide($scope.windowProvide)
211                    }
212
213                }
214
215                
216                
217                // 关闭编辑模式
218                $scope.closeModal = function () {
219                    $uibModalInstance.dismiss('cancel');
220                }
221
222            });
223        });
Full Screen

inject.spec.js

Source: inject.spec.js Github

copy
1import Vue from 'vue'
2import { Observer } from 'core/observer/index'
3import { isNative, isObject, hasOwn } from 'core/util/index'
4import testObjectOption from '../../../helpers/test-object-option'
5
6describe('Options provide/inject', () => {
7  testObjectOption('inject')
8
9  let injected
10  const injectedComp = {
11    inject: ['foo', 'bar'],
12    render () {},
13    created () {
14      injected = [this.foo, this.bar]
15    }
16  }
17
18  beforeEach(() => {
19    injected = null
20  })
21
22  it('should work', () => {
23    new Vue({
24      template: `<child/>`,
25      provide: {
26        foo: 1,
27        bar: false
28      },
29      components: {
30        child: {
31          template: `<injected-comp/>`,
32          components: {
33            injectedComp
34          }
35        }
36      }
37    }).$mount()
38
39    expect(injected).toEqual([1, false])
40  })
41
42  it('should use closest parent', () => {
43    new Vue({
44      template: `<child/>`,
45      provide: {
46        foo: 1,
47        bar: null
48      },
49      components: {
50        child: {
51          provide: {
52            foo: 3
53          },
54          template: `<injected-comp/>`,
55          components: {
56            injectedComp
57          }
58        }
59      }
60    }).$mount()
61
62    expect(injected).toEqual([3, null])
63  })
64
65  it('provide function', () => {
66    new Vue({
67      template: `<child/>`,
68      data: {
69        a: 1,
70        b: false
71      },
72      provide () {
73        return {
74          foo: this.a,
75          bar: this.b
76        }
77      },
78      components: {
79        child: {
80          template: `<injected-comp/>`,
81          components: {
82            injectedComp
83          }
84        }
85      }
86    }).$mount()
87
88    expect(injected).toEqual([1, false])
89  })
90
91  it('inject with alias', () => {
92    const injectAlias = {
93      inject: {
94        baz: 'foo',
95        qux: 'bar'
96      },
97      render () {},
98      created () {
99        injected = [this.baz, this.qux]
100      }
101    }
102
103    new Vue({
104      template: `<child/>`,
105      provide: {
106        foo: false,
107        bar: 2
108      },
109      components: {
110        child: {
111          template: `<inject-alias/>`,
112          components: {
113            injectAlias
114          }
115        }
116      }
117    }).$mount()
118
119    expect(injected).toEqual([false, 2])
120  })
121
122  it('inject before resolving data/props', () => {
123    const vm = new Vue({
124      provide: {
125        foo: 1
126      }
127    })
128
129    const child = new Vue({
130      parent: vm,
131      inject: ['foo'],
132      data () {
133        return {
134          bar: this.foo + 1
135        }
136      },
137      props: {
138        baz: {
139          default () {
140            return this.foo + 2
141          }
142        }
143      }
144    })
145
146    expect(child.foo).toBe(1)
147    expect(child.bar).toBe(2)
148    expect(child.baz).toBe(3)
149  })
150
151  // GitHub issue #5194
152  it('should work with functional', () => {
153    new Vue({
154      template: `<child/>`,
155      provide: {
156        foo: 1,
157        bar: false
158      },
159      components: {
160        child: {
161          functional: true,
162          inject: ['foo', 'bar'],
163          render (h, context) {
164            const { injections } = context
165            injected = [injections.foo, injections.bar]
166          }
167        }
168      }
169    }).$mount()
170
171    expect(injected).toEqual([1, false])
172  })
173
174  if (typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)) {
175    it('with Symbol keys', () => {
176      const s = Symbol()
177      const vm = new Vue({
178        template: `<child/>`,
179        provide: {
180          [s]: 123
181        },
182        components: {
183          child: {
184            inject: { s },
185            template: `<div>{{ s }}</div>`
186          }
187        }
188      }).$mount()
189      expect(vm.$el.textContent).toBe('123')
190    })
191
192    it('should merge symbol provide from mixins (functions)', () => {
193      const keyA = Symbol('foo')
194      const keyB = Symbol('bar')
195
196      const mixinA = { provide: () => ({ [keyA]: 'foo' }) }
197      const mixinB = { provide: () => ({ [keyB]: 'bar' }) }
198      const child = {
199        inject: {
200          foo: keyA,
201          bar: keyB
202        },
203        template: `<span/>`,
204        created () {
205          injected = [this.foo, this.bar]
206        }
207      }
208      new Vue({
209        mixins: [mixinA, mixinB],
210        render (h) {
211          return h(child)
212        }
213      }).$mount()
214
215      expect(injected).toEqual(['foo', 'bar'])
216    })
217  }
218
219  // GitHub issue #5223
220  it('should work with reactive array', done => {
221    const vm = new Vue({
222      template: `<div><child></child></div>`,
223      data () {
224        return {
225          foo: []
226        }
227      },
228      provide () {
229        return {
230          foo: this.foo
231        }
232      },
233      components: {
234        child: {
235          inject: ['foo'],
236          template: `<span>{{foo.length}}</span>`
237        }
238      }
239    }).$mount()
240
241    expect(vm.$el.innerHTML).toEqual(`<span>0</span>`)
242    vm.foo.push(vm.foo.length)
243    vm.$nextTick(() => {
244      expect(vm.$el.innerHTML).toEqual(`<span>1</span>`)
245      vm.foo.pop()
246      vm.$nextTick(() => {
247        expect(vm.$el.innerHTML).toEqual(`<span>0</span>`)
248        done()
249      })
250    })
251  })
252
253  it('should extend properly', () => {
254    const parent = Vue.extend({
255      template: `<span/>`,
256      inject: ['foo']
257    })
258
259    const child = parent.extend({
260      template: `<span/>`,
261      inject: ['bar'],
262      created () {
263        injected = [this.foo, this.bar]
264      }
265    })
266
267    new Vue({
268      template: `<div><parent/><child/></div>`,
269      provide: {
270        foo: 1,
271        bar: false
272      },
273      components: {
274        parent,
275        child
276      }
277    }).$mount()
278
279    expect(injected).toEqual([1, false])
280  })
281
282  it('should merge from mixins properly (objects)', () => {
283    const mixinA = { inject: { foo: 'foo' }}
284    const mixinB = { inject: { bar: 'bar' }}
285    const child = {
286      mixins: [mixinA, mixinB],
287      template: `<span/>`,
288      created () {
289        injected = [this.foo, this.bar]
290      }
291    }
292    new Vue({
293      provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
294      render (h) {
295        return h(child)
296      }
297    }).$mount()
298
299    expect(injected).toEqual(['foo', 'bar'])
300  })
301
302  it('should merge from mixins properly (arrays)', () => {
303    const mixinA = { inject: ['foo'] }
304    const mixinB = { inject: ['bar'] }
305    const child = {
306      mixins: [mixinA, mixinB],
307      inject: ['baz'],
308      template: `<span/>`,
309      created () {
310        injected = [this.foo, this.bar, this.baz]
311      }
312    }
313    new Vue({
314      provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
315      render (h) {
316        return h(child)
317      }
318    }).$mount()
319
320    expect(injected).toEqual(['foo', 'bar', 'baz'])
321  })
322
323  it('should merge from mixins properly (mix of objects and arrays)', () => {
324    const mixinA = { inject: { foo: 'foo' }}
325    const mixinB = { inject: ['bar'] }
326    const child = {
327      mixins: [mixinA, mixinB],
328      inject: { qux: 'baz' },
329      template: `<span/>`,
330      created () {
331        injected = [this.foo, this.bar, this.qux]
332      }
333    }
334    new Vue({
335      provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
336      render (h) {
337        return h(child)
338      }
339    }).$mount()
340
341    expect(injected).toEqual(['foo', 'bar', 'baz'])
342  })
343
344  it('should warn when injections has been modified', () => {
345    const key = 'foo'
346    const vm = new Vue({
347      provide: {
348        foo: 1
349      }
350    })
351
352    const child = new Vue({
353      parent: vm,
354      inject: ['foo']
355    })
356
357    expect(child.foo).toBe(1)
358    child.foo = 2
359    expect(
360      `Avoid mutating an injected value directly since the changes will be ` +
361      `overwritten whenever the provided component re-renders. ` +
362      `injection being mutated: "${key}"`).toHaveBeenWarned()
363  })
364
365  it('should warn when injections cannot be found', () => {
366    const vm = new Vue({})
367    new Vue({
368      parent: vm,
369      inject: ['foo', 'bar'],
370      created () {}
371    })
372    expect(`Injection "foo" not found`).toHaveBeenWarned()
373    expect(`Injection "bar" not found`).toHaveBeenWarned()
374  })
375
376  it('should not warn when injections can be found', () => {
377    const vm = new Vue({
378      provide: {
379        foo: 1,
380        bar: false,
381        baz: undefined
382      }
383    })
384    new Vue({
385      parent: vm,
386      inject: ['foo', 'bar', 'baz'],
387      created () {}
388    })
389    expect(`Injection "foo" not found`).not.toHaveBeenWarned()
390    expect(`Injection "bar" not found`).not.toHaveBeenWarned()
391    expect(`Injection "baz" not found`).not.toHaveBeenWarned()
392  })
393
394  it('should not warn when injection key which is not provided is not enumerable', () => {
395    const parent = new Vue({ provide: { foo: 1 }})
396    const inject = { foo: 'foo' }
397    Object.defineProperty(inject, '__ob__', { enumerable: false, value: '__ob__' })
398    new Vue({ parent, inject })
399    expect(`Injection "__ob__" not found`).not.toHaveBeenWarned()
400  })
401
402  // Github issue #6097
403  it('should not warn when injections cannot be found but have default value', () => {
404    const vm = new Vue({})
405    new Vue({
406      parent: vm,
407      inject: {
408        foo: { default: 1 },
409        bar: { default: false },
410        baz: { default: undefined }
411      },
412      created () {
413        injected = [this.foo, this.bar, this.baz]
414      }
415    })
416    expect(injected).toEqual([1, false, undefined])
417  })
418
419  it('should support name alias and default together', () => {
420    const vm = new Vue({
421      provide: {
422        FOO: 2
423      }
424    })
425    new Vue({
426      parent: vm,
427      inject: {
428        foo: { from: 'FOO', default: 1 },
429        bar: { default: false },
430        baz: { default: undefined }
431      },
432      created () {
433        injected = [this.foo, this.bar, this.baz]
434      }
435    })
436    expect(injected).toEqual([2, false, undefined])
437  })
438
439  it('should use provided value even if inject has default', () => {
440    const vm = new Vue({
441      provide: {
442        foo: 1,
443        bar: false,
444        baz: undefined
445      }
446    })
447    new Vue({
448      parent: vm,
449      inject: {
450        foo: { default: 2 },
451        bar: { default: 2 },
452        baz: { default: 2 }
453      },
454      created () {
455        injected = [this.foo, this.bar, this.baz]
456      }
457    })
458    expect(injected).toEqual([1, false, undefined])
459  })
460
461  // Github issue #6008
462  it('should merge provide from mixins (objects)', () => {
463    const mixinA = { provide: { foo: 'foo' }}
464    const mixinB = { provide: { bar: 'bar' }}
465    const child = {
466      inject: ['foo', 'bar'],
467      template: `<span/>`,
468      created () {
469        injected = [this.foo, this.bar]
470      }
471    }
472    new Vue({
473      mixins: [mixinA, mixinB],
474      render (h) {
475        return h(child)
476      }
477    }).$mount()
478
479    expect(injected).toEqual(['foo', 'bar'])
480  })
481
482  it('should merge provide from mixins (functions)', () => {
483    const mixinA = { provide: () => ({ foo: 'foo' }) }
484    const mixinB = { provide: () => ({ bar: 'bar' }) }
485    const child = {
486      inject: ['foo', 'bar'],
487      template: `<span/>`,
488      created () {
489        injected = [this.foo, this.bar]
490      }
491    }
492    new Vue({
493      mixins: [mixinA, mixinB],
494      render (h) {
495        return h(child)
496      }
497    }).$mount()
498
499    expect(injected).toEqual(['foo', 'bar'])
500  })
501
502  it('should merge provide from mixins (mix of objects and functions)', () => {
503    const mixinA = { provide: { foo: 'foo' }}
504    const mixinB = { provide: () => ({ bar: 'bar' }) }
505    const mixinC = { provide: { baz: 'baz' }}
506    const mixinD = { provide: () => ({ bam: 'bam' }) }
507    const child = {
508      inject: ['foo', 'bar', 'baz', 'bam'],
509      template: `<span/>`,
510      created () {
511        injected = [this.foo, this.bar, this.baz, this.bam]
512      }
513    }
514    new Vue({
515      mixins: [mixinA, mixinB, mixinC, mixinD],
516      render (h) {
517        return h(child)
518      }
519    }).$mount()
520
521    expect(injected).toEqual(['foo', 'bar', 'baz', 'bam'])
522  })
523
524  it('should merge provide from mixins and override existing keys', () => {
525    const mixinA = { provide: { foo: 'foo' }}
526    const mixinB = { provide: { foo: 'bar' }}
527    const child = {
528      inject: ['foo'],
529      template: `<span/>`,
530      created () {
531        injected = [this.foo]
532      }
533    }
534    new Vue({
535      mixins: [mixinA, mixinB],
536      render (h) {
537        return h(child)
538      }
539    }).$mount()
540
541    expect(injected).toEqual(['bar'])
542  })
543
544  it('should merge provide when Vue.extend', () => {
545    const mixinA = { provide: () => ({ foo: 'foo' }) }
546    const child = {
547      inject: ['foo', 'bar'],
548      template: `<span/>`,
549      created () {
550        injected = [this.foo, this.bar]
551      }
552    }
553    const Ctor = Vue.extend({
554      mixins: [mixinA],
555      provide: { bar: 'bar' },
556      render (h) {
557        return h(child)
558      }
559    })
560
561    new Ctor().$mount()
562
563    expect(injected).toEqual(['foo', 'bar'])
564  })
565
566  // #5913
567  it('should keep the reactive with provide', () => {
568    function isObserver (obj) {
569      if (isObject(obj)) {
570        return hasOwn(obj, '__ob__') && obj.__ob__ instanceof Observer
571      }
572      return false
573    }
574
575    const vm = new Vue({
576      template: `<div><child ref='child'></child></div>`,
577      data () {
578        return {
579          foo: {},
580          $foo: {},
581          foo1: []
582        }
583      },
584      provide () {
585        return {
586          foo: this.foo,
587          $foo: this.$foo,
588          foo1: this.foo1,
589          bar: {},
590          baz: []
591        }
592      },
593      components: {
594        child: {
595          inject: ['foo', '$foo', 'foo1', 'bar', 'baz'],
596          template: `<span/>`
597        }
598      }
599    }).$mount()
600    const child = vm.$refs.child
601    expect(isObserver(child.foo)).toBe(true)
602    expect(isObserver(child.$foo)).toBe(false)
603    expect(isObserver(child.foo1)).toBe(true)
604    expect(isObserver(child.bar)).toBe(false)
605    expect(isObserver(child.baz)).toBe(false)
606  })
607
608  // #6175
609  it('merge provide properly from mixins', () => {
610    const ProvideFooMixin = {
611      provide: {
612        foo: 'foo injected'
613      }
614    }
615
616    const ProvideBarMixin = {
617      provide: {
618        bar: 'bar injected'
619      }
620    }
621
622    const Child = {
623      inject: ['foo', 'bar'],
624      render (h) {
625        return h('div', [`foo: ${this.foo}, `, `bar: ${this.bar}`])
626      }
627    }
628
629    const Parent = {
630      mixins: [ProvideFooMixin, ProvideBarMixin],
631      render (h) {
632        return h(Child)
633      }
634    }
635
636    const vm = new Vue({
637      render (h) {
638        return h(Parent)
639      }
640    }).$mount()
641
642    expect(vm.$el.textContent).toBe(`foo: foo injected, bar: bar injected`)
643  })
644
645  it('merge provide with object syntax when using Vue.extend', () => {
646    const child = {
647      inject: ['foo'],
648      template: `<span/>`,
649      created () {
650        injected = this.foo
651      }
652    }
653    const Ctor = Vue.extend({
654      provide: { foo: 'foo' },
655      render (h) {
656        return h(child)
657      }
658    })
659
660    new Ctor().$mount()
661
662    expect(injected).toEqual('foo')
663  })
664
665  // #7284
666  it('should not inject prototype properties', () => {
667    const vm = new Vue({
668      provide: {}
669    })
670    new Vue({
671      parent: vm,
672      inject: ['constructor']
673    })
674    expect(`Injection "constructor" not found`).toHaveBeenWarned()
675  })
676})
677
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)