|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +// contributor license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright ownership. |
| 4 | +// The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +// (the "License"); you may not use this file except in compliance with |
| 6 | +// the License. You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 17 | +import { enableAutoUnmount, mount } from '@vue/test-utils'; |
| 18 | +import { nextTick } from 'vue'; |
| 19 | +import type { ProfileAnalyzationTree } from '@/api/client'; |
| 20 | +import { i18n } from '@/i18n'; |
| 21 | +import ProfileStackPanel from './ProfileStackPanel.vue'; |
| 22 | + |
| 23 | +const TREE: ProfileAnalyzationTree = { |
| 24 | + elements: [ |
| 25 | + { id: '1', parentId: '0', codeSignature: 'runtime.goexit:1695', duration: 10, durationChildExcluded: 0, count: 2 }, |
| 26 | + { id: '2', parentId: '1', codeSignature: 'main.handler:30', duration: 10, durationChildExcluded: 10, count: 2 }, |
| 27 | + ], |
| 28 | +}; |
| 29 | + |
| 30 | +function mountPanel(views?: ('tree' | 'flame')[]) { |
| 31 | + return mount(ProfileStackPanel, { |
| 32 | + props: { |
| 33 | + trees: [TREE], |
| 34 | + views, |
| 35 | + displayMode: views?.[0] ?? 'tree', |
| 36 | + hidePatterns: [], |
| 37 | + search: '', |
| 38 | + popoutTitle: 'pprof · go-service', |
| 39 | + }, |
| 40 | + attachTo: document.body, |
| 41 | + // d3-flame-graph needs a real layout engine; the panel is what is tested. |
| 42 | + global: { plugins: [i18n], stubs: { ProfileFlameGraph: true } }, |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +// Unmounted after each test: a mounted floating panel keeps a click listener |
| 47 | +// on document, which a later test's clicks would wake against a wiped body. |
| 48 | +enableAutoUnmount(afterEach); |
| 49 | +afterEach(() => { |
| 50 | + document.body.innerHTML = ''; |
| 51 | +}); |
| 52 | + |
| 53 | +describe('ProfileStackPanel', () => { |
| 54 | + it('offers the Tree | Flame switch only when the page has both views', () => { |
| 55 | + expect(mountPanel().findAll('.seg button').map((b) => b.text())).toEqual(['Tree', 'Flame']); |
| 56 | + expect(mountPanel(['flame', 'tree']).findAll('.seg button').map((b) => b.text())).toEqual(['Flame', 'Tree']); |
| 57 | + expect(mountPanel(['flame']).find('.seg').exists()).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it('keeps top slow to the Tree', () => { |
| 61 | + expect(mountPanel(['flame']).find('.psp-joined').exists()).toBe(false); |
| 62 | + expect(mountPanel(['tree']).find('.psp-joined').exists()).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it('pops out into a dialog with the same controls and no Pop out of its own', async () => { |
| 66 | + const w = mountPanel(['flame']); |
| 67 | + await w.find('.psp-pop').trigger('click'); |
| 68 | + await nextTick(); |
| 69 | + const dialog = document.body.querySelector('.modal'); |
| 70 | + expect(dialog?.querySelector('.modal__title')?.textContent).toBe('pprof · go-service'); |
| 71 | + expect(dialog?.querySelector('.psp-bar')).not.toBeNull(); |
| 72 | + expect(dialog?.querySelector('.psp-pop')).toBeNull(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('closes the pop-out when the result goes away', async () => { |
| 76 | + const w = mountPanel(['flame']); |
| 77 | + await w.find('.psp-pop').trigger('click'); |
| 78 | + await nextTick(); |
| 79 | + await w.setProps({ trees: [] }); |
| 80 | + await nextTick(); |
| 81 | + expect(document.body.querySelector('.modal')).toBeNull(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments