Skip to content

Commit

Permalink
update new dataset test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhendi committed Dec 18, 2024
1 parent 0144942 commit 3418a11
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
7 changes: 7 additions & 0 deletions frontend/setupTests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { config } from '@vue/test-utils';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import CsgButton from './src/components/shared/CsgButton.vue';
import SvgIcon from './src/components/shared/SvgIcon.vue';

config.global.plugins = [ElementPlus];

config.global.components = {
CsgButton,
SvgIcon
};
37 changes: 16 additions & 21 deletions frontend/src/components/__tests__/datasets/NewDataset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ vi.mock('../../../stores/UserStore', () => ({
}));

// Mock useFetchApi
const mockPost = vi.fn().mockResolvedValue({
json: async () => ({
data: { value: { data: { path: 'testuser/testdataset' } } },
error: { value: null }
})
});

vi.mock('../../../packs/useFetchApi', () => ({
default: () => ({
post: () => mockPost(),
post: () => ({
json: () => Promise.resolve({
data: { value: { data: { path: 'testuser/testdataset' } } },
error: { value: null }
})
})
})
}));

Expand Down Expand Up @@ -70,28 +68,25 @@ describe("NewDataset", () => {
});

describe("form validation", () => {
const validateForm = () => {
return new Promise(resolve => {
wrapper.vm.$refs.dataFormRef.validate(valid => resolve(valid))
})
}
it("validates required fields", async() => {
await wrapper.find('button').trigger('click');

await new Promise((resolve) => setTimeout(resolve, 300))
const formErrors = wrapper.findAll('.el-form-item__error');
expect(formErrors.length).toBeGreaterThan(0);
expect(await validateForm()).toBe(false)
})

it("accepts invalid dataset name", async () => {
wrapper.vm.dataForm.name = '**__invalid-name'
await wrapper.find('button').trigger('click');
await new Promise((resolve) => setTimeout(resolve, 300))
const errorMessage = wrapper.find('.el-form-item__error');
expect(errorMessage.exists()).toBe(true);
await wrapper.vm.$nextTick()
expect(await validateForm()).toBe(false)
});

it("accepts valid dataset name", async () => {
wrapper.vm.dataForm.name = 'valid-name'
await wrapper.find('button').trigger('click');
await new Promise((resolve) => setTimeout(resolve, 300))
const errorMessage = wrapper.find('.el-form-item__error');
expect(errorMessage.exists()).toBe(false);
await wrapper.vm.$nextTick()
expect(await validateForm()).toBe(true)
});
})

Expand Down
3 changes: 1 addition & 2 deletions frontend/src/components/datasets/NewDataset.vue
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@
body: JSON.stringify(params)
}
const newEndpoint = '/datasets'
const postres = await useFetchApi(newEndpoint, options).post().json()
const { data, error } = await postres.json()
const { data, error } = await useFetchApi(newEndpoint, options).post().json()
if (data.value) {
ElMessage.success(t('datasets.newDataset.createSuccess'))
Expand Down

0 comments on commit 3418a11

Please sign in to comment.