Skip to content

Commit c0762f7

Browse files
add jest to test galaxyproject#19841
1 parent 937c848 commit c0762f7

2 files changed

Lines changed: 44 additions & 12 deletions

File tree

client/src/components/Form/Elements/FormDirectory.test.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,28 @@ jest.mock("app");
1414

1515
const { server, http } = useServerMock();
1616

17+
async function init(wrapper, data) {
18+
// the file dialog modal should exist
19+
const filesDialogComponent = wrapper.findComponent(FilesDialog);
20+
expect(filesDialogComponent.exists()).toBe(true);
21+
filesDialogComponent.vm.callback({ url: data.url });
22+
// HACK to avoid https://github.com/facebook/jest/issues/2549 (URL implementation is not the same as global node)
23+
wrapper.vm.pathChunks = data.pathChunks;
24+
await flushPromises();
25+
await validateLatestEmittedPath(wrapper, data.url);
26+
}
27+
28+
async function validateLatestEmittedPath(wrapper, expectedPath) {
29+
const latestEmitIndex = wrapper.emitted()["input"].length - 1;
30+
const latestPath = wrapper.emitted()["input"][latestEmitIndex][0];
31+
expect(latestPath).toBe(expectedPath);
32+
33+
// also manually change prop value to be able to test the value being displayed
34+
await wrapper.setProps({ value: latestPath });
35+
const fullPathDisplayed = wrapper.find("[data-description='directory full path']");
36+
expect(fullPathDisplayed.text()).toBe(`Directory Path: ${expectedPath}`);
37+
}
38+
1739
describe("DirectoryPathEditableBreadcrumb", () => {
1840
let wrapper;
1941
let spyOnUrlSet;
@@ -29,6 +51,11 @@ describe("DirectoryPathEditableBreadcrumb", () => {
2951
const validPath = "validpath";
3052
const invalidPath = "./];";
3153

54+
const testingDataWithSpecialChars = {
55+
url: "gxfiles://directory/sub directory/with%20percent",
56+
pathChunks: [{ pathChunk: "directory" }, { pathChunk: "sub%20directory" }, { pathChunk: "with%2520percent" }],
57+
};
58+
3259
const saveNewChunk = async (path) => {
3360
// enter a new path chunk
3461
const input = wrapper.find("#path-input-breadcrumb");
@@ -38,15 +65,6 @@ describe("DirectoryPathEditableBreadcrumb", () => {
3865
input.trigger("keyup.enter");
3966
return input;
4067
};
41-
const init = async () => {
42-
// the file dialog modal should exist
43-
const filesDialogComponent = wrapper.findComponent(FilesDialog);
44-
expect(filesDialogComponent.exists()).toBe(true);
45-
filesDialogComponent.vm.callback({ url: testingData.url });
46-
// HACK to avoid https://github.com/facebook/jest/issues/2549 (URL implementation is not the same as global node)
47-
wrapper.vm.pathChunks = testingData.pathChunks;
48-
await flushPromises();
49-
};
5068

5169
beforeEach(async () => {
5270
spyOnUrlSet = jest.spyOn(FormDirectory.methods, "setUrl");
@@ -67,13 +85,12 @@ describe("DirectoryPathEditableBreadcrumb", () => {
6785

6886
wrapper = mount(FormDirectory, {
6987
propsData: {
70-
callback: () => {},
88+
value: null,
7189
},
7290
localVue: localVue,
7391
pinia,
7492
});
7593
await flushPromises();
76-
await init();
7794
});
7895
afterEach(async () => {
7996
if (wrapper) {
@@ -83,6 +100,7 @@ describe("DirectoryPathEditableBreadcrumb", () => {
83100
});
84101

85102
it("should render Breadcrumb", async () => {
103+
await init(wrapper, testingData);
86104
// after initial folder is chosen, setUrl() should be called and modal disappear
87105
expect(spyOnUrlSet).toHaveBeenCalled();
88106
expect(wrapper.findComponent(FilesDialog).exists()).toBe(false);
@@ -107,6 +125,7 @@ describe("DirectoryPathEditableBreadcrumb", () => {
107125
});
108126

109127
it("should prevent invalid Paths", async () => {
128+
await init(wrapper, testingData);
110129
// enter a new path chunk
111130
const input = await saveNewChunk(invalidPath);
112131
await flushPromises();
@@ -117,6 +136,7 @@ describe("DirectoryPathEditableBreadcrumb", () => {
117136
});
118137

119138
it("should save and remove new Paths", async () => {
139+
await init(wrapper, testingData);
120140
// enter a new path chunk
121141
const input = await saveNewChunk(validPath);
122142

@@ -129,19 +149,31 @@ describe("DirectoryPathEditableBreadcrumb", () => {
129149
expect(wrapper.findAll("li.breadcrumb-item").length).toBe(testingData.expectedNumberOfPaths + 1);
130150
// find newly added chunk
131151
const addedChunk = wrapper.findAll("li.breadcrumb-item button").wrappers.find((e) => e.text() === validPath);
152+
153+
await validateLatestEmittedPath(wrapper, `${testingData.url}/${validPath}`);
154+
132155
// remove chunk from the path
133156
await addedChunk.trigger("click");
134157
await flushPromises();
135158
// number of elements should be the same again
136159
expect(wrapper.findAll("li.breadcrumb-item").length).toBe(testingData.expectedNumberOfPaths);
160+
161+
await validateLatestEmittedPath(wrapper, testingData.url);
137162
});
138163

139164
it("should update new path", async () => {
165+
await init(wrapper, testingData);
140166
// enter a new path chunk
141167
expect(spyOnUpdateURL).toHaveBeenCalled();
142168
await saveNewChunk(validPath);
143169
await flushPromises();
144170

145171
expect(spyOnUpdateURL).toHaveBeenCalled();
146172
});
173+
174+
it("should retain special characters in path", async () => {
175+
// the init function itself validates that the emits and path display
176+
// retain special characters in the url
177+
await init(wrapper, testingDataWithSpecialChars);
178+
});
147179
});

client/src/components/Form/Elements/FormDirectory.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</b-breadcrumb-item>
4040
</b-breadcrumb>
4141

42-
<div v-if="value" class="px-2">
42+
<div v-if="value" class="px-2" data-description="directory full path">
4343
<span v-localize>Directory Path:</span>
4444
<code>{{ value }}</code>
4545
</div>

0 commit comments

Comments
 (0)