From 4e1fc4814be0e4cc3891fad0a0ec764b9212ccfd Mon Sep 17 00:00:00 2001 From: Lucas Di Pentima Date: Fri, 26 Mar 2021 16:02:29 -0300 Subject: [PATCH] 17337: Improves unit test readability & expandability. Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima --- .../collection-service-files-response.test.ts | 91 ++++++++++++++----- 1 file changed, 70 insertions(+), 21 deletions(-) diff --git a/src/services/collection-service/collection-service-files-response.test.ts b/src/services/collection-service/collection-service-files-response.test.ts index 17a1f388..b2480c4b 100644 --- a/src/services/collection-service/collection-service-files-response.test.ts +++ b/src/services/collection-service/collection-service-files-response.test.ts @@ -8,30 +8,79 @@ import { getFileFullPath, extractFilesData } from './collection-service-files-re describe('collection-service-files-response', () => { describe('extractFilesData', () => { - it('should extract', () => { - // given - const xmlString = '/c=xxxxx-zzzzz-vvvvvvvvvvvvvvv/Wed, 24 Feb 2021 22:16:19 GMTHTTP/1.1 200 OK/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/21582976"1666cee048aa7f98182780"2Wed, 24 Feb 2021 22:16:19 GMTtext/plain; charset=utf-8HTTP/1.1 200 OK/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/table%201%202%203133352"1666cee048aa7f98208e8"table 1 2 3Wed, 24 Feb 2021 22:16:19 GMTtext/plain; charset=utf-8HTTP/1.1 200 OK'; - const parser = new DOMParser(); - const xmlDoc = parser.parseFromString(xmlString, "text/xml"); + it('should correctly decode URLs & file names', () => { + const testCases = [ + // input URL, input display name, expected URL, expected name + ['table%201%202%203', 'table 1 2 3', 'table 1 2 3', 'table 1 2 3'], + ['table%25&%3F%2A2', 'table%&?*2', 'table%&?*2', 'table%&?*2'], + ["G%C3%BCnter%27s%20file.pdf", "Günter's file.pdf", "Günter's file.pdf", "Günter's file.pdf"], + ['G%25C3%25BCnter%27s%2520file.pdf', 'G%C3%BCnter's%20file.pdf', "G%C3%BCnter's%20file.pdf", "G%C3%BCnter's%20file.pdf"] + ]; - // when - const result = extractFilesData(xmlDoc); + testCases.forEach(([inputURL, inputDisplayName, expectedURL, expectedName]) => { + // given + const collUUID = 'xxxxx-zzzzz-vvvvvvvvvvvvvvv'; + const xmlData = ` + + + + /c=xxxxx-zzzzz-vvvvvvvvvvvvvvv/ + + + + + + + + + + + + + + + + + Fri, 26 Mar 2021 14:44:08 GMT + + HTTP/1.1 200 OK + + + + /c=${collUUID}/${inputURL} + + + + application/pdf + + + + + + + + + + + ${inputDisplayName} + 3 + Fri, 26 Mar 2021 14:44:08 GMT + "166feb9c9110c008325a59" + + HTTP/1.1 200 OK + + + + `; + const parser = new DOMParser(); + const xmlDoc = parser.parseFromString(xmlData, "text/xml"); - // then - expect(result).toEqual([{ id: "zzzzz-xxxxx-vvvvvvvvvvvvvvv/2", name: "2", path: "", size: 1582976, type: "file", url: "/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/2" }, { id: "zzzzz-xxxxx-vvvvvvvvvvvvvvv/table 1 2 3", name: "table 1 2 3", path: "", size: 133352, type: "file", url: "/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/table 1 2 3" }]); - }); + // when + const result = extractFilesData(xmlDoc); - it('should extract ecoded data and do not encode already encoded props', () => { - // given - const xmlString = '/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/Fri, 26 Mar 2021 11:45:50 GMTHTTP/1.1 200 OK/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/table%25&%3F%2A23Fri, 26 Mar 2021 11:45:50 GMT"166fe1e1a403fb683"text/plain; charset=utf-8table%&?*2HTTP/1.1 200 OK'; - const parser = new DOMParser(); - const xmlDoc = parser.parseFromString(xmlString, "text/xml"); - - // when - const result = extractFilesData(xmlDoc); - - // then - expect(result).toEqual([{ id: "zzzzz-xxxxx-vvvvvvvvvvvvvvv/table%&?*2", name: "table%&?*2", path: "", size: 3, type: "file", url: "/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/table%&?*2" }]); + // then + expect(result).toEqual([{ id: `${collUUID}/${expectedURL}`, name: expectedName, path: "", size: 3, type: "file", url: `/c=${collUUID}/${expectedURL}` }]); + }); }); }); -- 2.30.2