-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathunittests.js
More file actions
1 lines (1 loc) · 16.9 KB
/
unittests.js
File metadata and controls
1 lines (1 loc) · 16.9 KB
1
define(function(require,exports,module){var MasterDocumentManager=brackets.getModule("document/DocumentManager"),MasterMainViewManager=brackets.getModule("view/MainViewManager"),SpecRunnerUtils=brackets.getModule("spec/SpecRunnerUtils"),UrlCodeHints=require("main");describe("LegacyInteg:Url Code Hinting",function(){var extensionTestPath=SpecRunnerUtils.getTestPath("/spec/extn-urlcodehints-testfiles/"),testHtmlPath=extensionTestPath+"testfiles/test.html",testCssPath=extensionTestPath+"testfiles/subfolder/test.css",testScssPath=extensionTestPath+"testfiles/subfolder/test.scss",testDocument,testEditor,hintsObj,testfilesDirHints=["subfolder/","test.html"],subfolderDirHints=["chevron.png","test.css","test.js","test.scss"],UrlCodeHintsDirHintsMac=["../testfiles/"],UrlCodeHintsDirHints=["../testfiles/"];function createMockEditor(doc){return SpecRunnerUtils.createMockEditorForDocument(doc)}async function setupTests(testFilePath){MasterDocumentManager.getDocumentForPath(testFilePath).done(function(doc){testDocument=doc}),await awaitsFor(function(){return testDocument},"Unable to open test document"),testEditor=createMockEditor(testDocument),MasterMainViewManager._edit(MasterMainViewManager.ACTIVE_PANE,testDocument)}function tearDownTests(){MasterMainViewManager._closeAll(MasterMainViewManager.ALL_PANES),testDocument&&SpecRunnerUtils.destroyMockEditor(testDocument),testEditor=null,testDocument=null,hintsObj=null}async function expectAsyncHints(provider){expect(provider.hasHints(testEditor,null)).toBe(!0),hintsObj=provider.getHints(),expect(hintsObj).toBeTruthy(),hintsObj instanceof Object&&hintsObj.hasOwnProperty("done")&&hintsObj.done(function(resolvedHintsObj){hintsObj=resolvedHintsObj}),await awaitsFor(function(){return!hintsObj||hintsObj.hints},"Unable to resolve hints")}function expectNoHints(provider){expect(provider.hasHints(testEditor,null)).toBeFalsy()}function verifyUrlHints(hintList,expectedHints){expect(hintList).toEqual(expectedHints)}function fixPos(pos){return"sticky"in pos||(pos.sticky=null),pos}describe("HTML Url Code Hints",function(){beforeAll(async function(){await setupTests(testHtmlPath)}),afterAll(function(){tearDownTests()}),it("should hint for href attribute",async function(){testEditor.setCursorPos({line:14,ch:12}),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),verifyUrlHints(hintsObj.hints,testfilesDirHints)}),it("should hint for src attribute",async function(){testEditor.setCursorPos({line:15,ch:13}),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),verifyUrlHints(hintsObj.hints,testfilesDirHints)}),it("should hint for poster attribute",async function(){testEditor.setCursorPos({line:24,ch:17}),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),verifyUrlHints(hintsObj.hints,testfilesDirHints)}),it("should not hint for type attribute",async function(){testEditor.setCursorPos({line:15,ch:21}),expectNoHints(UrlCodeHints.hintProvider)}),it("should not hint in query part of url",async function(){testEditor.setCursorPos({line:20,ch:31}),expectNoHints(UrlCodeHints.hintProvider)}),it("should hint up 1 folder for '../'",async function(){testEditor.setCursorPos({line:21,ch:14}),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider);var expectedHints="win"!==brackets.platform?UrlCodeHintsDirHintsMac:UrlCodeHintsDirHints;verifyUrlHints(hintsObj.hints,expectedHints)})}),describe("CSS Url Code Hints",function(){beforeAll(async function(){await setupTests(testHtmlPath)}),afterAll(function(){tearDownTests()}),it("should hint for @import url()",async function(){testEditor.setCursorPos({line:4,ch:12}),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),verifyUrlHints(hintsObj.hints,testfilesDirHints)}),it("should hint for background-image: url()",async function(){testEditor.setCursorPos({line:6,ch:24}),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),verifyUrlHints(hintsObj.hints,testfilesDirHints)}),it("should hint for border-image: url('')",async function(){testEditor.setCursorPos({line:7,ch:21}),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),verifyUrlHints(hintsObj.hints,testfilesDirHints)}),it('should hint for list-style-image: url("")',async function(){testEditor.setCursorPos({line:8,ch:25}),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),verifyUrlHints(hintsObj.hints,testfilesDirHints)}),it("should not hint for @import outside of url()",async function(){testEditor.setCursorPos({line:4,ch:15}),expectNoHints(UrlCodeHints.hintProvider)}),it("should not hint for background-image outside of url()",async function(){testEditor.setCursorPos({line:11,ch:20}),expectNoHints(UrlCodeHints.hintProvider)})}),describe("Url Code Hints in a subfolder",function(){afterEach(function(){tearDownTests()}),it("should hint for background-image: url() in CSS",async function(){await setupTests(testCssPath),testEditor.setCursorPos({line:3,ch:26}),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),verifyUrlHints(hintsObj.hints,subfolderDirHints)}),it("should hint for background-image: url() in SCSS",async function(){await setupTests(testScssPath),testEditor.setCursorPos({line:4,ch:34}),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),verifyUrlHints(hintsObj.hints,subfolderDirHints)})}),describe("Url Insertion",function(){beforeEach(async function(){await setupTests(testHtmlPath)}),afterEach(function(){tearDownTests()}),it("should handle unclosed url(",async function(){var pos1={line:11,ch:20},pos2={line:11,ch:24},pos3={line:11,ch:34};testEditor.setCursorPos(pos1),testDocument.replaceRange("url(",pos1,pos1),testEditor.setCursorPos(pos2),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(2),expect(hintsObj.hints[1]).toBe("test.html"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[1])).toBe(!1),expect(testDocument.getRange(pos1,pos3)).toEqual("url(test.html)"),expect(fixPos(testEditor.getCursorPos())).toEql(fixPos(pos3))}),it("should handle unclosed url( with unclosed single-quote",async function(){var pos1={line:11,ch:20},pos2={line:11,ch:25},pos3={line:11,ch:36};testEditor.setCursorPos(pos1),testDocument.replaceRange("url('",pos1,pos1),testEditor.setCursorPos(pos2),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(2),expect(hintsObj.hints[1]).toBe("test.html"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[1])).toBe(!1),expect(testDocument.getRange(pos1,pos3)).toEql("url('test.html')"),expect(fixPos(testEditor.getCursorPos())).toEql(fixPos(pos3))}),it("should keep hints open after inserting folder in HTML",async function(){var pos1={line:18,ch:12},pos2={line:18,ch:22},pos3={line:18,ch:33},pos4={line:18,ch:34};testEditor.setCursorPos(pos1),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(2),expect(hintsObj.hints[0]).toBe("subfolder/"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0])).toBe(!0),expect(testDocument.getRange(pos1,pos2)).toEqual("subfolder/"),expect(fixPos(testEditor.getCursorPos())).toEql(fixPos(pos2)),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(subfolderDirHints.length),expect(hintsObj.hints[0]).toBe("subfolder/chevron.png"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0])).toBe(!1),expect(testDocument.getRange(pos1,pos3)).toEqual("subfolder/chevron.png"),expect(fixPos(testEditor.getCursorPos())).toEql(fixPos(pos4))}),it("should keep hints open after inserting folder in CSS",async function(){var pos1={line:11,ch:20},pos2={line:11,ch:25},pos3={line:11,ch:35},pos4={line:11,ch:37},pos5={line:11,ch:48};testEditor.setCursorPos(pos1),testDocument.replaceRange('url("',pos1,pos1),testEditor.setCursorPos(pos2),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(2),expect(hintsObj.hints[0]).toBe("subfolder/"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0])).toBe(!0),expect(testDocument.getRange(pos1,pos4)).toEqual('url("subfolder/")'),expect(fixPos(testEditor.getCursorPos())).toEql(fixPos(pos3)),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(subfolderDirHints.length),expect(hintsObj.hints[0]).toBe("subfolder/chevron.png"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0])).toBe(!1),expect(testDocument.getRange(pos1,pos5)).toEqual('url("subfolder/chevron.png")'),expect(fixPos(testEditor.getCursorPos())).toEql(fixPos(pos5))}),it("should insert folder and replace file in HTML",async function(){var pos1={line:23,ch:11},pos2={line:23,ch:21},pos3={line:23,ch:31},pos4={line:23,ch:32};testEditor.setCursorPos(pos1),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(2),expect(hintsObj.hints[0]).toBe("subfolder/"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0])).toBe(!0),expect(testDocument.getRange(pos1,pos3)).toEqual("subfolder/test2.html"),expect(fixPos(testEditor.getCursorPos())).toEql(fixPos(pos2)),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(subfolderDirHints.length),expect(hintsObj.hints[0]).toBe("subfolder/chevron.png"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0])).toBe(!1),expect(testDocument.getRange(pos1,pos4)).toEqual("subfolder/chevron.png")}),it("should completely replace file in HTML",async function(){var pos1={line:25,ch:11},pos2={line:25,ch:27},pos3={line:25,ch:34};testEditor.setCursorPos(pos2),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(1),expect(hintsObj.hints[0]).toBe("subfolder/chevron.png"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0])).toBe(!1),expect(testDocument.getRange(pos1,pos3)).toEqual("'subfolder/chevron.png'"),expect(fixPos(testEditor.getCursorPos())).toEql(fixPos(pos3))}),it("should insert filtered folder in HTML",async function(){var pos1={line:23,ch:11},pos2={line:23,ch:14},pos3={line:23,ch:31};testDocument.replaceRange("sub",pos1,pos1),testEditor.setCursorPos(pos2),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(1),expect(hintsObj.hints[0]).toBe("subfolder/"),UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0]),expect(testDocument.getRange(pos1,pos3)).toEqual("subfolder/test2.html")}),it("should replace filtered file in HTML",async function(){var pos1={line:23,ch:11},pos2={line:23,ch:14},pos3={line:23,ch:21};testDocument.replaceRange("tes",pos1,pos1),testEditor.setCursorPos(pos2),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(1),expect(hintsObj.hints[0]).toBe("test.html"),UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0]),expect(testDocument.getRange(pos1,pos3)).toEqual("test.html'")}),it("should insert folder and replace file in CSS",async function(){var pos1={line:10,ch:24},pos2={line:10,ch:34},pos3={line:10,ch:43},pos4={line:10,ch:45};testEditor.setCursorPos(pos1),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(2),expect(hintsObj.hints[0]).toBe("subfolder/"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0])).toBe(!0),expect(testDocument.getRange(pos1,pos3)).toEqual("subfolder/dummy.jpg"),expect(fixPos(testEditor.getCursorPos())).toEql(fixPos(pos2)),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(subfolderDirHints.length),expect(hintsObj.hints[0]).toBe("subfolder/chevron.png"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0])).toBe(!1),expect(testDocument.getRange(pos1,pos4)).toEqual("subfolder/chevron.png")}),it("should insert filtered folder in CSS",async function(){var pos1={line:10,ch:24},pos2={line:10,ch:27},pos3={line:10,ch:43};testDocument.replaceRange("sub",pos1,pos1),testEditor.setCursorPos(pos2),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(1),expect(hintsObj.hints[0]).toBe("subfolder/"),UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0]),expect(testDocument.getRange(pos1,pos3)).toEqual("subfolder/dummy.jpg")}),it("should replace filtered file in CSS",async function(){var pos1={line:10,ch:24},pos2={line:10,ch:27},pos3={line:10,ch:34};testDocument.replaceRange("tes",pos1,pos1),testEditor.setCursorPos(pos2),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(1),expect(hintsObj.hints[0]).toBe("test.html"),UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0]),expect(testDocument.getRange(pos1,pos3)).toEqual("test.html)")}),it("should collapse consecutive path separators when inserting folder in HTML",async function(){var pos1={line:22,ch:11},pos2={line:22,ch:22};testEditor.setCursorPos(pos1),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(2),expect(hintsObj.hints[0]).toBe("subfolder/"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0])).toBe(!0),expect(testDocument.getRange(pos1,pos2)).toEqual("subfolder/'")}),it("should collapse consecutive path separators when inserting folder in CSS",async function(){var pos1={line:9,ch:15},pos2={line:9,ch:26};testEditor.setCursorPos(pos1),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(2),expect(hintsObj.hints[0]).toBe("subfolder/"),expect(UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0])).toBe(!0),expect(testDocument.getRange(pos1,pos2)).toEqual('subfolder/"')}),it("should show & insert case insensitive hints in HTML",async function(){var pos1={line:18,ch:12},pos2={line:18,ch:13},pos3={line:18,ch:21};testDocument.replaceRange("T",pos1,pos1),testEditor.setCursorPos(pos2),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(1),expect(hintsObj.hints[0]).toBe("test.html"),UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0]),expect(testDocument.getRange(pos1,pos3)).toEqual("test.html")}),it("should show & insert case insensitive hints in CSS",async function(){var pos1={line:6,ch:24},pos2={line:6,ch:25},pos3={line:6,ch:33};testDocument.replaceRange("T",pos1,pos1),testEditor.setCursorPos(pos2),hintsObj=null,await expectAsyncHints(UrlCodeHints.hintProvider),expect(hintsObj).toBeTruthy(),expect(hintsObj.hints).toBeTruthy(),expect(hintsObj.hints.length).toBe(1),expect(hintsObj.hints[0]).toBe("test.html"),UrlCodeHints.hintProvider.insertHint(hintsObj.hints[0]),expect(testDocument.getRange(pos1,pos3)).toEqual("test.html")})}),describe("Project root relative Url Code Hints",function(){var testWindow,brackets,CodeHintManager,CommandManager,Commands,FileViewController,EditorManager;it("should hint site root '/'",async function(){if(Phoenix.isNativeApp&&"linux"===Phoenix.platform)return;let hintList;testWindow=await SpecRunnerUtils.createTestWindowAndRun(),brackets=testWindow.brackets,CodeHintManager=brackets.test.CodeHintManager,CommandManager=brackets.test.CommandManager,FileViewController=brackets.test.FileViewController,Commands=brackets.test.Commands,EditorManager=brackets.test.EditorManager,await SpecRunnerUtils.loadProjectInTestWindow(extensionTestPath),await awaitsForDone(FileViewController.openAndSelectDocument(testHtmlPath,FileViewController.WORKING_SET_VIEW)),(testEditor=EditorManager.getActiveEditor()).setCursorPos({line:22,ch:12}),await awaitsFor(async function(){return await awaitsForDone(CommandManager.execute(Commands.SHOW_CODE_HINTS)),(hintList=CodeHintManager._getCodeHintList())&&hintList.hints&&hintList.hints.includes("/testfiles/")},"waiting for code hints to be there",5e3,100),expect(hintList).toBeTruthy(),expect(hintList.hints).toBeTruthy(),expect(hintList.hints).toContain("/testfiles/"),testEditor=null,testDocument=null,testWindow=null,brackets=null,CodeHintManager=null,FileViewController=null,CommandManager=null,Commands=null,EditorManager=null,await SpecRunnerUtils.closeTestWindow()},3e4)})})});