Leo
Leo
NNuxt
Created by Leo on 11/17/2024 in #❓・help
Vitest times out when mocking the Router
Hey, I'm currently trying to mock useRouter().push(), but unfortunately, Vitest (the mountSuspended function) times out.
// Some stuff
const mockRouterPush = vi.fn();

mockNuxtImport('useRouter', () => {
return () => ({
push: mockRouterPush,
});
});

describe('CreateDaytisticContent', () => {
beforeEach(() => {
vi.clearAllMocks();
// Setup successful API response
mockCreateDaytistic.mockResolvedValue({
status: 201,
_data: { id: '123' },
});
});

it('submits form successfully when date is selected', async () => {
const wrapper = await mountSuspended(CreateDaytisticContent);
const testDate = new Date('2024-01-01');

// First, set the modelValue prop
await wrapper.findComponent(DatePicker).setValue(testDate);

// Then emit the update event
await wrapper.findComponent(DatePicker).vm.$emit('update', testDate);

// Wait for Vue to process the updates
await wrapper.vm.$nextTick();

// Trigger form submission
await wrapper.find('form').trigger('submit');

// Wait for all promises to resolve
await flushPromises();

// Verify API call was made with correct date
expect(mockCreateDaytistic).toHaveBeenCalledWith(testDate);

// Verify success flow
expect(mockRouterPush).toHaveBeenCalled();
expect(mockSuccess).toHaveBeenCalled();
});


});
// Some stuff
const mockRouterPush = vi.fn();

mockNuxtImport('useRouter', () => {
return () => ({
push: mockRouterPush,
});
});

describe('CreateDaytisticContent', () => {
beforeEach(() => {
vi.clearAllMocks();
// Setup successful API response
mockCreateDaytistic.mockResolvedValue({
status: 201,
_data: { id: '123' },
});
});

it('submits form successfully when date is selected', async () => {
const wrapper = await mountSuspended(CreateDaytisticContent);
const testDate = new Date('2024-01-01');

// First, set the modelValue prop
await wrapper.findComponent(DatePicker).setValue(testDate);

// Then emit the update event
await wrapper.findComponent(DatePicker).vm.$emit('update', testDate);

// Wait for Vue to process the updates
await wrapper.vm.$nextTick();

// Trigger form submission
await wrapper.find('form').trigger('submit');

// Wait for all promises to resolve
await flushPromises();

// Verify API call was made with correct date
expect(mockCreateDaytistic).toHaveBeenCalledWith(testDate);

// Verify success flow
expect(mockRouterPush).toHaveBeenCalled();
expect(mockSuccess).toHaveBeenCalled();
});


});
5 replies
NNuxt
Created by Leo on 11/10/2024 in #❓・help
Internal server error: Attempted to assign to readonly property.
No description
5 replies
NNuxt
Created by Leo on 10/30/2024 in #❓・help
No Cookies sent in Header?
Hello everyone, does anyone have any idea why no cookies are sent when a request is made via the plugin? It doesn't work in the ssr or the spa. https://paste.gg/p/anonymous/c723ccf5af994f65a376dc56c2909d08 Thank you in advance!
13 replies