|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { mount } from '@vue/test-utils' |
| 3 | +import IndexPage from '../index.vue' |
| 4 | + |
| 5 | +const mockSayHello = vi.fn() |
| 6 | + |
| 7 | +// useGrpcClient is auto-imported by Nuxt, so stub it as a global |
| 8 | +vi.stubGlobal('useGrpcClient', vi.fn(() => Promise.resolve({ sayHello: mockSayHello }))) |
| 9 | + |
| 10 | +const stubs = { |
| 11 | + Card: { template: '<div><slot /></div>' }, |
| 12 | + CardHeader: { template: '<div><slot /></div>' }, |
| 13 | + CardTitle: { template: '<h1><slot /></h1>' }, |
| 14 | + CardDescription: { template: '<p><slot /></p>' }, |
| 15 | + CardContent: { template: '<div><slot /></div>' }, |
| 16 | + CardFooter: { template: '<div><slot /></div>' }, |
| 17 | + Button: { template: '<button @click="$emit(\'click\')"><slot /></button>' }, |
| 18 | + Badge: { template: '<span><slot /></span>' }, |
| 19 | + Alert: { template: '<div><slot /></div>' }, |
| 20 | + AlertTitle: { template: '<div><slot /></div>' }, |
| 21 | + AlertDescription: { template: '<div><slot /></div>' }, |
| 22 | +} |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + mockSayHello.mockReset() |
| 26 | +}) |
| 27 | + |
| 28 | +describe('Index Page', () => { |
| 29 | + it('renders the title and description', () => { |
| 30 | + const wrapper = mount(IndexPage, { global: { stubs } }) |
| 31 | + |
| 32 | + expect(wrapper.text()).toContain('Bufstack') |
| 33 | + expect(wrapper.text()).toContain('Rust gRPC + Nuxt 4 + Protocol Buffers') |
| 34 | + }) |
| 35 | + |
| 36 | + it('calls sayHello and displays the response', async () => { |
| 37 | + mockSayHello.mockResolvedValue({ message: 'Hello World!' }) |
| 38 | + |
| 39 | + const wrapper = mount(IndexPage, { global: { stubs } }) |
| 40 | + |
| 41 | + await wrapper.find('button').trigger('click') |
| 42 | + |
| 43 | + // Wait for the async gRPC call to resolve and DOM to update |
| 44 | + await vi.waitFor(() => { |
| 45 | + expect(wrapper.text()).toContain('Hello World!') |
| 46 | + }) |
| 47 | + }) |
| 48 | +}) |
0 commit comments