|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +const fs = require("fs"); |
| 6 | +const path = require("path"); |
| 7 | + |
| 8 | +describe("update (XMLgenerator.js)", () => { |
| 9 | + let update; |
| 10 | + let $; |
| 11 | + |
| 12 | + beforeAll(() => { |
| 13 | + // Minimal jQuery mock |
| 14 | + $ = () => ({ |
| 15 | + find: (selector) => ({ |
| 16 | + val: () => { |
| 17 | + const map = { |
| 18 | + ".channelcontrols": "ctrl1, ctrl2", |
| 19 | + ".channelgroupwidtho": "rep1, rep2", |
| 20 | + ".channeldescription": "desc", |
| 21 | + ".channelrecordnumber": "42", |
| 22 | + ".channelhexcolor": "#ff0000", |
| 23 | + ".channelbamType": "Google Drive", |
| 24 | + ".channelbamlink": "https://drive.google.com/file/d/abc", |
| 25 | + ".channeltotalreadsmapped": "12345", |
| 26 | + ".channelreadmapmethod": "STAR", |
| 27 | + ".channelpublicationlink": "https://pub.com/xyz", |
| 28 | + ".channeltissue": "Leaf", |
| 29 | + ".channelsvgname": "ath-leaf.svg", |
| 30 | + ".channeltitle": "Test Title", |
| 31 | + ".channelsralink": "SRR000001", |
| 32 | + ".channelspecies": "Arabidopsis", |
| 33 | + ".channelforeground": "#000000", |
| 34 | + ".channelfilename": "file1.bam", |
| 35 | + }; |
| 36 | + return map[selector] || "dummy"; |
| 37 | + }, |
| 38 | + }), |
| 39 | + }); |
| 40 | + |
| 41 | + // Robust document.getElementById mock for all ids |
| 42 | + const getElementByIdMock = (id) => { |
| 43 | + const values = { |
| 44 | + reqxml: { value: "TestXML" }, |
| 45 | + reqauthor: { value: "Author" }, |
| 46 | + contectinfo: { value: "contact@email.com" }, |
| 47 | + }; |
| 48 | + return values[id] || { value: "dummy" }; |
| 49 | + }; |
| 50 | + if (global.document) { |
| 51 | + global.document.getElementById = getElementByIdMock; |
| 52 | + } else { |
| 53 | + global.document = { getElementById: getElementByIdMock }; |
| 54 | + } |
| 55 | + global.$ = $; |
| 56 | + |
| 57 | + // Required top-level variables for update() |
| 58 | + global.topXML = [ |
| 59 | + '\t\t<file info="<?channeldescription?>" record_number="<?channelrecordnumber?>" foreground="<?channelforeground?>" hex_colour="<?channelhexcolor?>" bam_type="<?channelbamType?>" name="<?channelbamlink?>" filename="<?channelfilename?>" total_reads_mapped="<?channeltotalreadsmapped?>" read_map_method="<?channelreadmapmethod?>" publication_link="<?channelpublicationlink?>" svg_subunit="<?channeltissue?>" svgname="<?channelsvgname?>" description="<?channeltitle?>" url="<?channelsralink?>" species="<?channelspecies?>" title="<?channeligbtitle?>">', |
| 60 | + "\t\t\t<controls>\n", |
| 61 | + ].join("\r\n"); |
| 62 | + global.controlsXML = ""; |
| 63 | + global.replicatesXML = ["\t\t\t</controls>", "\t\t\t<groupwith>\n"].join("\r\n"); |
| 64 | + global.endingXML = ["\t\t\t</groupwith>", "\t\t</file>", "\n"].join("\r\n"); |
| 65 | + global.existingXML = ""; |
| 66 | + global.all_controls = ""; |
| 67 | + global.all_replicates = ""; |
| 68 | + |
| 69 | + // Load the update function from XMLgenerator.js |
| 70 | + const code = fs.readFileSync(path.join(__dirname, "../Submission_page/XMLgenerator.js"), "utf8"); |
| 71 | + const fnMatch = code.match(/function update\s*\(([^)]*)\)\s*{([\s\S]*?)^}/m); |
| 72 | + if (!fnMatch) throw new Error("update function not found in XMLgenerator.js"); |
| 73 | + const args = fnMatch[1]; |
| 74 | + const body = fnMatch[2]; |
| 75 | + |
| 76 | + update = new Function(args, body); |
| 77 | + }); |
| 78 | + |
| 79 | + it("generates correct XML for given form values", () => { |
| 80 | + const v = {}; // dummy, not used in our $ mock |
| 81 | + const result = update("", v); |
| 82 | + // Check for key XML elements and values |
| 83 | + expect(result).toContain('info="desc"'); |
| 84 | + expect(result).toContain('record_number="42"'); |
| 85 | + expect(result).toContain('hex_colour="#ff0000"'); |
| 86 | + expect(result).toContain('bam_type="Google Drive"'); |
| 87 | + expect(result).toContain('name="https://drive.google.com/file/d/abc"'); |
| 88 | + expect(result).toContain('filename="file1.bam"'); |
| 89 | + expect(result).toContain("<bam_exp>ctrl1</bam_exp>"); |
| 90 | + expect(result).toContain("<bam_exp>ctrl2</bam_exp>"); |
| 91 | + expect(result).toContain("<bam_exp>rep1</bam_exp>"); |
| 92 | + expect(result).toContain("<bam_exp>rep2</bam_exp>"); |
| 93 | + }); |
| 94 | +}); |
0 commit comments