|
| 1 | +import { defineComponent, h } from 'vue' |
| 2 | +import { mount } from '@vue/test-utils' |
| 3 | +import { initializeVuePlugin } from '../../../test/initializeVuePlugin' |
| 4 | +import { mockClock } from '../../../../core/test' |
| 5 | +// eslint-disable-next-line camelcase |
| 6 | +import { UNSTABLE_useVueComponentTracker } from './useVueComponentTracker' |
| 7 | + |
| 8 | +const MOUNT_DURATION = 50 |
| 9 | + |
| 10 | +describe('UNSTABLE_useVueComponentTracker', () => { |
| 11 | + it('reports a vueComponentRender vital on mount', async () => { |
| 12 | + const addDurationVitalSpy = jasmine.createSpy() |
| 13 | + const clock = mockClock() |
| 14 | + initializeVuePlugin({ publicApi: { addDurationVital: addDurationVitalSpy } }) |
| 15 | + |
| 16 | + const TrackedComponent = defineComponent({ |
| 17 | + setup() { |
| 18 | + UNSTABLE_useVueComponentTracker('MyComponent') |
| 19 | + }, |
| 20 | + render() { |
| 21 | + clock.tick(MOUNT_DURATION) |
| 22 | + return h('div') |
| 23 | + }, |
| 24 | + }) |
| 25 | + |
| 26 | + // eslint-disable-next-line @typescript-eslint/await-thenable |
| 27 | + await mount(TrackedComponent) |
| 28 | + |
| 29 | + expect(addDurationVitalSpy).toHaveBeenCalledTimes(1) |
| 30 | + const [name, options] = addDurationVitalSpy.calls.mostRecent().args |
| 31 | + expect(name).toBe('vueComponentRender') |
| 32 | + expect(options).toEqual({ |
| 33 | + description: 'MyComponent', |
| 34 | + startTime: clock.timeStamp(0), |
| 35 | + duration: MOUNT_DURATION, |
| 36 | + context: { |
| 37 | + is_first_render: true, |
| 38 | + framework: 'vue', |
| 39 | + }, |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + it('reports is_first_render: false on update', async () => { |
| 44 | + const addDurationVitalSpy = jasmine.createSpy() |
| 45 | + initializeVuePlugin({ publicApi: { addDurationVital: addDurationVitalSpy } }) |
| 46 | + |
| 47 | + const TrackedComponent = defineComponent({ |
| 48 | + props: ['count'], |
| 49 | + setup() { |
| 50 | + UNSTABLE_useVueComponentTracker('MyComponent') |
| 51 | + }, |
| 52 | + render() { |
| 53 | + return h('div', this.count) |
| 54 | + }, |
| 55 | + }) |
| 56 | + |
| 57 | + // eslint-disable-next-line @typescript-eslint/await-thenable |
| 58 | + const wrapper = await mount(TrackedComponent, { props: { count: 0 } }) |
| 59 | + await wrapper.setProps({ count: 1 }) |
| 60 | + |
| 61 | + expect(addDurationVitalSpy).toHaveBeenCalledTimes(2) |
| 62 | + const options = addDurationVitalSpy.calls.mostRecent().args[1] |
| 63 | + expect(options.context.is_first_render).toBe(false) |
| 64 | + }) |
| 65 | +}) |
0 commit comments