|
| 1 | +/* eslint-disable no-unused-vars */ |
| 2 | +/* eslint-disable no-undef */ |
| 3 | + |
| 4 | +import RayVue3Plugin from '../src/RayVue3'; |
| 5 | +import { FakeRay } from './TestClasses/FakeRay'; |
| 6 | +import { FakeVm } from './TestClasses/FakeVm'; |
| 7 | +import { Vue3RayMixin } from '../src/v3/Vue3RayMixin'; |
| 8 | +import { VueRay } from '../src/shared/VueRay'; |
| 9 | + |
| 10 | +let ray: FakeRay, fakeApp: FakeApp; |
| 11 | + |
| 12 | +class FakeApp { |
| 13 | + public providedItems: any[] = []; |
| 14 | + public config: any; |
| 15 | + public mixins: any = {}; |
| 16 | + |
| 17 | + constructor() { |
| 18 | + this.providedItems = []; |
| 19 | + this.config = { |
| 20 | + globalProperties: { |
| 21 | + $ray: null, |
| 22 | + }, |
| 23 | + errorHandler: null, |
| 24 | + }; |
| 25 | + |
| 26 | + this.mixins = {}; |
| 27 | + this.mixins.methods = { |
| 28 | + $ray: null, |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + provide(name: any, options: any) { |
| 33 | + this.providedItems.push(name); |
| 34 | + } |
| 35 | + |
| 36 | + getProvided() { |
| 37 | + return this.providedItems; |
| 38 | + } |
| 39 | + |
| 40 | + getLastProvided() { |
| 41 | + return this.getProvided().slice(0).pop(); |
| 42 | + } |
| 43 | + |
| 44 | + mixin(obj: any) { |
| 45 | + this.mixins = Object.assign({}, this.mixins, obj); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +beforeEach(() => { |
| 50 | + ray = new FakeRay(); |
| 51 | + fakeApp = new FakeApp(); |
| 52 | +}); |
| 53 | + |
| 54 | +const prepareMixin = () => { |
| 55 | + // @ts-ignore |
| 56 | + Vue3RayMixin.$options = { |
| 57 | + name: 'test', |
| 58 | + __file: 'test.js', |
| 59 | + }; |
| 60 | + |
| 61 | + // @ts-ignore |
| 62 | + Vue3RayMixin.$ray = function (...args: any[]) { |
| 63 | + return ray.send(...args); |
| 64 | + }; |
| 65 | + |
| 66 | + Vue3RayMixin.methods = { |
| 67 | + $ray(...args: any[]) { |
| 68 | + return ray.send(...args); |
| 69 | + }, |
| 70 | + }; |
| 71 | +}; |
| 72 | + |
| 73 | +describe('Vue 3 Ray Plugin:', () => { |
| 74 | + test('it installs successfully', () => { |
| 75 | + RayVue3Plugin.install(fakeApp, {}); |
| 76 | + |
| 77 | + expect(fakeApp.getLastProvided()).toBe('ray'); |
| 78 | + expect(fakeApp.getProvided().length).toBe(1); |
| 79 | + //expect(fakeApp.config.globalProperties['$ray']).not.toBe(null); |
| 80 | + expect(fakeApp.mixins.methods.$ray).not.toBe(null); |
| 81 | + |
| 82 | + if (fakeApp.mixins.methods.$ray !== null) { |
| 83 | + const testRay = fakeApp.mixins.methods.$ray; |
| 84 | + |
| 85 | + expect(typeof testRay).not.toBe('undefined'); |
| 86 | + expect(testRay().constructor.name).toBe('VueRay'); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + test('it installs successfully with the interceptErrors option', () => { |
| 91 | + RayVue3Plugin.install(fakeApp, { interceptErrors: true }); |
| 92 | + |
| 93 | + expect(fakeApp.getLastProvided()).toBe('ray'); |
| 94 | + expect(fakeApp.getProvided().length).toBe(1); |
| 95 | + expect(fakeApp.mixins.methods.$ray).not.toBe(null); |
| 96 | + expect(fakeApp.config.errorHandler).not.toBeNull(); |
| 97 | + expect(typeof fakeApp.config.errorHandler).toBe('function'); |
| 98 | + |
| 99 | + const fakeVm = new FakeVm(ray); |
| 100 | + fakeApp.config.errorHandler({ stack: 'test error' }, fakeVm); |
| 101 | + |
| 102 | + expect(ray.payloads.length).toBe(3); |
| 103 | + expect(ray.payloads[0].content).toContain('test error'); |
| 104 | + expect(ray.payloads[1].content).toBe('small'); |
| 105 | + expect(ray.payloads[2].content).toBe('red'); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('Vue 3 Mixin:', () => { |
| 110 | + test('it does not conditionally display events if not defined', () => { |
| 111 | + VueRay.show_component_lifecycles = []; |
| 112 | + |
| 113 | + prepareMixin(); |
| 114 | + |
| 115 | + //Vue3RayMixin.beforeCreate(); |
| 116 | + Vue3RayMixin.beforeMount(); |
| 117 | + Vue3RayMixin.created(); |
| 118 | + Vue3RayMixin.mounted(); |
| 119 | + Vue3RayMixin.unmounted(); |
| 120 | + Vue3RayMixin.updated(); |
| 121 | + |
| 122 | + expect(ray.payloads.length).toBe(0); |
| 123 | + }); |
| 124 | + |
| 125 | + test('it conditionally displays the beforeMount event', () => { |
| 126 | + VueRay.show_component_lifecycles = ['before-mount']; |
| 127 | + |
| 128 | + prepareMixin(); |
| 129 | + Vue3RayMixin.beforeMount(); |
| 130 | + |
| 131 | + expect(ray.payloads.length).toBe(1); |
| 132 | + expect(ray.payloads).toMatchSnapshot(); |
| 133 | + }); |
| 134 | + |
| 135 | + test('it conditionally displays the created & mounted events', () => { |
| 136 | + VueRay.show_component_lifecycles = ['created', 'mounted']; |
| 137 | + |
| 138 | + prepareMixin(); |
| 139 | + Vue3RayMixin.created(); |
| 140 | + Vue3RayMixin.mounted(); |
| 141 | + |
| 142 | + expect(ray.payloads.length).toBe(2); |
| 143 | + expect(ray.payloads).toMatchSnapshot(); |
| 144 | + }); |
| 145 | + |
| 146 | + test('it conditionally displays the unmounted event', () => { |
| 147 | + VueRay.show_component_lifecycles = ['unmounted']; |
| 148 | + |
| 149 | + prepareMixin(); |
| 150 | + Vue3RayMixin.unmounted(); |
| 151 | + |
| 152 | + expect(ray.payloads.length).toBe(1); |
| 153 | + expect(ray.payloads).toMatchSnapshot(); |
| 154 | + }); |
| 155 | + |
| 156 | + test('it conditionally displays the updated event', () => { |
| 157 | + VueRay.show_component_lifecycles = ['updated']; |
| 158 | + |
| 159 | + prepareMixin(); |
| 160 | + Vue3RayMixin.updated(); |
| 161 | + |
| 162 | + expect(ray.payloads.length).toBe(1); |
| 163 | + expect(ray.payloads).toMatchSnapshot(); |
| 164 | + }); |
| 165 | +}); |
0 commit comments