codesnippet.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright 2018-2022 Elyra Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. describe('Code Snippet tests', () => {
  17. const snippetName = 'test-code-snippet';
  18. beforeEach(() => {
  19. cy.resetJupyterLab();
  20. openCodeSnippetExtension();
  21. });
  22. afterEach(() => {
  23. // delete code-snippet used for testing
  24. cy.exec(`elyra-metadata remove code-snippets --name=${snippetName}`, {
  25. failOnNonZeroExit: false
  26. });
  27. });
  28. it('should open the Code Snippet extension', () => {
  29. // make sure it is rendered properly
  30. cy.get('.elyra-metadata .elyra-metadataHeader').contains('Code Snippets');
  31. // and code-snippet create new button is visible
  32. cy.findByRole('button', { name: /create new code snippet/i }).should(
  33. 'be.visible'
  34. );
  35. });
  36. it('should provide warnings when required fields are not entered properly', () => {
  37. createInvalidCodeSnippet(snippetName);
  38. // Metadata editor should not close
  39. cy.get('.lm-TabBar-tabLabel')
  40. .contains('New Code Snippet')
  41. .should('be.visible');
  42. // Fields marked as required should be highlighted
  43. cy.get('.error-detail li.text-danger').as('required-warnings');
  44. cy.get('@required-warnings').should('have.length', 2);
  45. });
  46. it('should create valid code-snippet', () => {
  47. createValidCodeSnippet(snippetName);
  48. // Metadata editor tab should not be visible
  49. cy.get('.lm-TabBar-tabLabel')
  50. .contains('New Code Snippet')
  51. .should('not.exist');
  52. // Check new code snippet is displayed
  53. getSnippetByName(snippetName);
  54. });
  55. it('should fail to create duplicate Code Snippet', () => {
  56. // create code snippet
  57. createValidCodeSnippet(snippetName);
  58. // tries to create duplicate code snippet
  59. createValidCodeSnippet(snippetName);
  60. // Should display dialog
  61. cy.get('.jp-Dialog-header').contains('Error making request');
  62. // Close dialog
  63. cy.get('button.jp-mod-accept').click();
  64. });
  65. it('should trigger save / submit on pressing enter', () => {
  66. populateCodeSnippetFields(snippetName);
  67. cy.get('.elyra-formEditor-form-display_name').type('{enter}');
  68. // Metadata editor tab should not be visible
  69. cy.get('.lm-TabBar-tabLabel')
  70. .contains('New Code Snippet')
  71. .should('not.exist');
  72. // Check new code snippet is displayed
  73. getSnippetByName(snippetName);
  74. });
  75. // Delete snippet
  76. it('should delete existing Code Snippet', () => {
  77. createValidCodeSnippet(snippetName);
  78. cy.wait(500);
  79. getSnippetByName(snippetName);
  80. deleteSnippet(snippetName);
  81. });
  82. // Duplicate snippet
  83. it('should duplicate existing Code Snippet', () => {
  84. createValidCodeSnippet(snippetName);
  85. cy.wait(500);
  86. let snippetRef = getSnippetByName(snippetName);
  87. expect(snippetRef).to.not.be.null;
  88. // create a duplicate of this snippet
  89. duplicateSnippet(snippetName);
  90. cy.wait(100);
  91. snippetRef = getSnippetByName(`${snippetName}-Copy1`);
  92. expect(snippetRef).to.not.be.null;
  93. // create another duplicate of this snippet
  94. duplicateSnippet(snippetName);
  95. cy.wait(100);
  96. snippetRef = getSnippetByName(`${snippetName}-Copy2`);
  97. expect(snippetRef).to.not.be.null;
  98. // cleanup
  99. deleteSnippet(snippetName);
  100. deleteSnippet(`${snippetName}-Copy1`);
  101. deleteSnippet(`${snippetName}-Copy2`);
  102. });
  103. it('should have visible action buttons for existing code snippet', () => {
  104. createValidCodeSnippet(snippetName);
  105. const actionButtons = getActionButtonsElement(snippetName);
  106. const buttonTitles = [
  107. 'Copy to clipboard',
  108. 'Insert',
  109. 'Edit',
  110. 'Duplicate',
  111. 'Delete'
  112. ];
  113. // Check expected buttons to be visible
  114. buttonTitles.forEach((title: string) => {
  115. actionButtons.within(() => {
  116. cy.get(`button[title="${title}"]`).should('be.visible');
  117. });
  118. });
  119. });
  120. it('should display/hide code snippet content on expand/collapse button', () => {
  121. createValidCodeSnippet(snippetName);
  122. // Check new code snippet is displayed
  123. const item = getSnippetByName(snippetName);
  124. // Click on expand button
  125. item
  126. .parentsUntil('.elyra-metadata-item')
  127. .first()
  128. .find('button')
  129. .first()
  130. .click();
  131. // Check code mirror is visible
  132. cy.get('.elyra-expandableContainer-details-visible').should('exist');
  133. // Click on collapse button
  134. item
  135. .parentsUntil('.elyra-metadata-item')
  136. .first()
  137. .find('button')
  138. .first()
  139. .click();
  140. // Check code mirror is not visible
  141. cy.get('.elyra-expandableContainer-details-visible').should('not.exist');
  142. });
  143. it('should update code snippet name after editing it', () => {
  144. createValidCodeSnippet(snippetName);
  145. // Find new snippet in display and click on edit button
  146. getActionButtonsElement(snippetName).within(() => {
  147. cy.get('button[title="Edit"]').click();
  148. });
  149. // Edit snippet name
  150. const newSnippetName = 'new-name';
  151. cy.get('.elyra-formEditor-form-display_name')
  152. .find('input')
  153. .clear()
  154. .type(newSnippetName);
  155. saveAndCloseMetadataEditor();
  156. cy.wait(500);
  157. // Check new snippet name is displayed
  158. const updatedSnippetItem = getSnippetByName(newSnippetName);
  159. // Check old snippet name does not exist
  160. expect(updatedSnippetItem.innerText).not.to.eq(snippetName);
  161. // Delete updated code snippet
  162. deleteSnippet(newSnippetName);
  163. });
  164. it('should fail to insert a code snippet into unsupported widget', () => {
  165. createValidCodeSnippet(snippetName);
  166. // Insert snippet into launcher widget
  167. insert(snippetName);
  168. // Check if insertion failed and dismiss dialog
  169. cy.get('.jp-Dialog-header').contains('Error');
  170. cy.get('button.jp-mod-accept').click();
  171. cy.wait(100);
  172. });
  173. it('should insert a python code snippet into python editor', () => {
  174. createValidCodeSnippet(snippetName);
  175. // Open blank python file
  176. cy.createNewScriptEditor('Python');
  177. cy.wait(1500);
  178. // Insert snippet into python editor
  179. insert(snippetName);
  180. // Check if editor has the new code
  181. cy.get('.CodeMirror:visible');
  182. cy.get('span.cm-string').contains(/test/i);
  183. });
  184. it('should fail to insert a java code snippet into python editor', () => {
  185. createValidCodeSnippet(snippetName, 'Java');
  186. // Open blank python file
  187. cy.createNewScriptEditor('Python');
  188. cy.wait(500);
  189. // Insert snippet into python editor
  190. insert(snippetName);
  191. // Check for language mismatch warning
  192. cy.get('.jp-Dialog-header').contains(/warning/i);
  193. // Dismiss the dialog
  194. cy.findByRole('button', { name: /cancel/i }).click();
  195. // Check it did not insert the code
  196. cy.get('.CodeMirror:visible');
  197. cy.get('span.cm-string').should('not.exist');
  198. });
  199. // DEV NOTE: Uncomment the tests below to run them locally
  200. // TODO: Investigate tests below only failing on CI
  201. // Steps: checkCodeMirror, closeTabWithoutSaving
  202. // it('Test inserting a code snippet into a notebook', () => {
  203. // openCodeSnippetExtension();
  204. // clickCreateNewSnippetButton();
  205. // const snippetName = 'test-code-snippet';
  206. // fillMetadaEditorForm(snippetName);
  207. // cy.wait(500);
  208. // // Open blank notebook file
  209. // cy.get(
  210. // '.jp-LauncherCard[data-category="Notebook"][title="Python 3"]:visible'
  211. // ).click();
  212. // cy.wait(500);
  213. // // Check widget is loaded
  214. // cy.get('.CodeMirror:visible');
  215. // insert(snippetName);
  216. // // Check if notebook cell has the new code
  217. // checkCodeMirror();
  218. // // NOTE: Notebook cell is still empty when this test runs on CI
  219. // closeTabWithoutSaving();
  220. // // NOTE: Save dialog isn't visible when this test runs on CI
  221. // });
  222. // it('Test inserting a code snippet into a markdown file', () => {
  223. // openCodeSnippetExtension();
  224. // clickCreateNewSnippetButton();
  225. // const snippetName = 'test-code-snippet';
  226. // fillMetadaEditorForm(snippetName);
  227. // cy.wait(500);
  228. // // Open blank notebook file
  229. // cy.get(
  230. // '.jp-LauncherCard[title="Create a new markdown file"]:visible'
  231. // ).click();
  232. // cy.wait(500);
  233. // // Check widget is loaded
  234. // cy.get('.CodeMirror:visible');
  235. // insert(snippetName);
  236. // // Check if notebook cell has the new code
  237. // checkCodeMirror();
  238. // // Check for language decoration
  239. // cy.get('span.cm-comment')
  240. // .first()
  241. // .contains('Python');
  242. // closeTabWithoutSaving();
  243. // });
  244. });
  245. // ------------------------------
  246. // ----- Utility Functions ------
  247. // ------------------------------
  248. const openCodeSnippetExtension = (): void => {
  249. cy.get('.jp-SideBar [title="Code Snippets"]').click();
  250. cy.get('.jp-SideBar .lm-mod-current[title="Code Snippets"]');
  251. };
  252. const getSnippetByName = (snippetName: string): any => {
  253. return cy.get(`[data-item-id="${snippetName}"]`);
  254. };
  255. const createInvalidCodeSnippet = (snippetName: string): any => {
  256. clickCreateNewSnippetButton();
  257. // Name code snippet
  258. cy.get('.elyra-formEditor-form-display_name').type(snippetName);
  259. saveAndCloseMetadataEditor();
  260. };
  261. const populateCodeSnippetFields = (
  262. snippetName: string,
  263. language?: string
  264. ): any => {
  265. clickCreateNewSnippetButton();
  266. // Name code snippet
  267. cy.get('.elyra-formEditor-form-display_name').type(snippetName);
  268. // Select python language from dropdown list
  269. editSnippetLanguage(snippetName, language ?? 'Python');
  270. // Add snippet code
  271. cy.get('.CodeMirror .CodeMirror-scroll:visible').type(
  272. 'print("Code Snippet Test")'
  273. );
  274. };
  275. const createValidCodeSnippet = (
  276. snippetName: string,
  277. language?: string
  278. ): any => {
  279. populateCodeSnippetFields(snippetName, language);
  280. saveAndCloseMetadataEditor();
  281. cy.wait(1000);
  282. };
  283. const clickCreateNewSnippetButton = (): void => {
  284. cy.findByRole('button', { name: /create new code snippet/i }).click();
  285. };
  286. const saveAndCloseMetadataEditor = (): void => {
  287. cy.get('.elyra-metadataEditor-saveButton > button:visible').click();
  288. };
  289. const deleteSnippet = (snippetName: string): void => {
  290. // Find element by name
  291. const item = getSnippetByName(snippetName);
  292. // Click on delete button
  293. item.find('button[title="Delete"]').click();
  294. // Confirm action in dialog
  295. cy.get('.jp-Dialog-header').contains(`Delete snippet '${snippetName}'?`);
  296. cy.get('button.jp-mod-accept').click();
  297. };
  298. const duplicateSnippet = (snippetName: string): void => {
  299. // Find element by name
  300. const item = getSnippetByName(snippetName);
  301. // Click duplicate button
  302. item.find('button[title="Duplicate"]').click();
  303. };
  304. const getActionButtonsElement = (snippetName: string): any => {
  305. const actionButtonsElement = getSnippetByName(snippetName).find(
  306. '.elyra-expandableContainer-action-buttons'
  307. );
  308. return actionButtonsElement;
  309. };
  310. const insert = (snippetName: string): void => {
  311. getActionButtonsElement(snippetName).within(() => {
  312. cy.get('button[title="Insert"]').click();
  313. });
  314. cy.wait(500);
  315. };
  316. const editSnippetLanguage = (snippetName: string, lang: string): void => {
  317. cy.get('.elyra-formEditor')
  318. .find('.elyra-form-DropDown-item option')
  319. .then(list => Cypress._.map(list, 'value'))
  320. .should('include', lang);
  321. cy.get('.elyra-formEditor')
  322. .find('.elyra-formEditor-form-language input')
  323. .type(lang);
  324. cy.get('.elyra-formEditor-form-language input')
  325. .should('have.value', lang)
  326. .click();
  327. };