|
| 1 | +import { mount } from '@vue/test-utils'; |
| 2 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 3 | +import RunModeBar from '../components/RunModeBar.vue'; |
| 4 | + |
| 5 | +vi.setConfig({ testTimeout: 10000 }); |
| 6 | + |
| 7 | +vi.useFakeTimers(); |
| 8 | + |
| 9 | +const mockFetch = vi.fn(); |
| 10 | +vi.stubGlobal('$fetch', mockFetch); |
| 11 | + |
| 12 | +describe('RunModeBar.vue', () => { |
| 13 | + beforeEach(() => { |
| 14 | + mockFetch.mockReset(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('renders correctly for Develop', async () => { |
| 18 | + const wrapper = mount(RunModeBar, { |
| 19 | + props: { runMode: 'Develop' }, |
| 20 | + global: { stubs: { Teleport: true } }, |
| 21 | + }); |
| 22 | + await vi.runAllTimersAsync(); |
| 23 | + expect(wrapper.html()).toContain('#3b82f6'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('renders correctly for Test', async () => { |
| 27 | + const wrapper = mount(RunModeBar, { |
| 28 | + props: { runMode: 'Test' }, |
| 29 | + global: { stubs: { Teleport: true } }, |
| 30 | + }); |
| 31 | + await vi.runAllTimersAsync(); |
| 32 | + expect(wrapper.html()).toContain('#22c55e'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('renders with custom barSize', async () => { |
| 36 | + const wrapper = mount(RunModeBar, { |
| 37 | + props: { runMode: 'Test', barSize: 10 }, |
| 38 | + global: { stubs: { Teleport: true } }, |
| 39 | + }); |
| 40 | + await vi.runAllTimersAsync(); |
| 41 | + expect(wrapper.html()).toContain('10px'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('hides and reappears after custom time', async () => { |
| 45 | + const wrapper = mount(RunModeBar, { |
| 46 | + props: { runMode: 'Develop', barHide: 1 }, |
| 47 | + global: { stubs: { Teleport: true } }, |
| 48 | + }); |
| 49 | + await vi.runAllTimersAsync(); |
| 50 | + |
| 51 | + const div = wrapper.find('div'); |
| 52 | + expect(div.exists()).toBe(true); |
| 53 | + await div.trigger('click'); |
| 54 | + expect((wrapper.vm as SafeAny).runModeStyle.display).toBe('none'); |
| 55 | + |
| 56 | + await vi.advanceTimersByTimeAsync(2000); |
| 57 | + expect((wrapper.vm as SafeAny).runModeStyle.display).toBe('block'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('uses fallback API when runMode is not provided', async () => { |
| 61 | + mockFetch.mockResolvedValueOnce('dev'); |
| 62 | + const wrapper = mount(RunModeBar, { |
| 63 | + props: {}, |
| 64 | + global: { stubs: { Teleport: true } }, |
| 65 | + }); |
| 66 | + await vi.runAllTimersAsync(); |
| 67 | + expect(mockFetch).toHaveBeenCalled(); |
| 68 | + expect(wrapper.html()).toContain('#3b82f6'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('accepts async function as runMode', async () => { |
| 72 | + const wrapper = mount(RunModeBar, { |
| 73 | + props: { runMode: async () => 'Test' }, |
| 74 | + global: { stubs: { Teleport: true } }, |
| 75 | + }); |
| 76 | + await vi.runAllTimersAsync(); |
| 77 | + expect(wrapper.html()).toContain('#22c55e'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('renders nothing when unknown runMode is given', async () => { |
| 81 | + const wrapper = mount(RunModeBar, { |
| 82 | + props: { runMode: 'Nothing' }, |
| 83 | + global: { stubs: { Teleport: true } }, |
| 84 | + }); |
| 85 | + await vi.runAllTimersAsync(); |
| 86 | + expect(wrapper.html()).not.toContain('background-color'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('supports left side bar rendering', async () => { |
| 90 | + const wrapper = mount(RunModeBar, { |
| 91 | + props: { runMode: 'Develop', barSide: 'left', barSize: 5 }, |
| 92 | + global: { stubs: { Teleport: true } }, |
| 93 | + }); |
| 94 | + await vi.runAllTimersAsync(); |
| 95 | + expect(wrapper.html()).toContain('width: 5px'); |
| 96 | + }); |
| 97 | +}); |
0 commit comments