|
| 1 | +/* global cy */ |
| 2 | +import { t } from 'i18next'; |
| 3 | +import CodePanel from './CodePanel'; |
| 4 | +import '@ui5/webcomponents-icons/dist/AllIcons.js'; |
| 5 | + |
| 6 | +describe('CodePanel Component', () => { |
| 7 | + it('renders text-only code response when no language is provided', () => { |
| 8 | + const code = 'const hello = "world";'; |
| 9 | + |
| 10 | + cy.mount(<CodePanel code={code} language="" />); |
| 11 | + |
| 12 | + cy.get('.code-response').should('exist'); |
| 13 | + cy.get('#copy-icon').should('exist'); |
| 14 | + cy.get('#code-text').should('contain.text', code); |
| 15 | + cy.get('ui5-panel').should('not.exist'); |
| 16 | + cy.get('pre').should('not.exist'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('renders a code panel with syntax highlighting when language is provided', () => { |
| 20 | + const code = 'const hello = "world";'; |
| 21 | + const language = 'javascript'; |
| 22 | + |
| 23 | + cy.mount(<CodePanel code={code} language={language} />); |
| 24 | + |
| 25 | + cy.get('.code-panel').should('exist'); |
| 26 | + cy.get('ui5-panel').should('exist'); |
| 27 | + cy.get('ui5-title').should('contain.text', language); |
| 28 | + cy.get('pre').should('exist'); |
| 29 | + cy.get('code').should('contain.text', code); |
| 30 | + }); |
| 31 | + |
| 32 | + it('displays only the copy button when withAction is false or link is not provided', () => { |
| 33 | + const code = 'const hello = "world";'; |
| 34 | + const language = 'javascript'; |
| 35 | + |
| 36 | + cy.mount(<CodePanel code={code} language={language} withAction={false} />); |
| 37 | + |
| 38 | + cy.get('.action-button').should('have.length', 1); |
| 39 | + cy.get('.action-button') |
| 40 | + .eq(0) |
| 41 | + .should('have.attr', 'icon', 'copy'); |
| 42 | + cy.get('.action-button') |
| 43 | + .eq(0) |
| 44 | + .should('contain.text', t('common.buttons.copy')); |
| 45 | + }); |
| 46 | + |
| 47 | + it('displays both copy and place buttons when withAction is true and link is provided', () => { |
| 48 | + const code = 'const hello = "world";'; |
| 49 | + const language = 'javascript'; |
| 50 | + const link = { |
| 51 | + name: 'my-service', |
| 52 | + address: 'namespaces/default/services/my-service', |
| 53 | + actionType: 'New', |
| 54 | + }; |
| 55 | + |
| 56 | + cy.mount( |
| 57 | + <CodePanel |
| 58 | + code={code} |
| 59 | + language={language} |
| 60 | + withAction={true} |
| 61 | + link={link} |
| 62 | + />, |
| 63 | + ); |
| 64 | + |
| 65 | + cy.get('.action-button').should('have.length', 2); |
| 66 | + cy.get('.action-button') |
| 67 | + .eq(0) |
| 68 | + .should('have.attr', 'icon', 'copy') |
| 69 | + .should('have.attr', 'design', 'Transparent') |
| 70 | + .should('have.attr', 'accessible-name', t('common.buttons.copy')); |
| 71 | + cy.get('.action-button') |
| 72 | + .eq(1) |
| 73 | + .should('have.attr', 'icon', 'sys-add'); |
| 74 | + cy.get('.action-button') |
| 75 | + .eq(1) |
| 76 | + .should('contain.text', t('common.buttons.place')); |
| 77 | + }); |
| 78 | + |
| 79 | + it('renders the place button with correct attributes for namespace resources', () => { |
| 80 | + const code = 'apiVersion: v1\nkind: Service\nmetadata:\n name: my-service'; |
| 81 | + const language = 'yaml'; |
| 82 | + const link = { |
| 83 | + name: 'my-service', |
| 84 | + address: 'namespaces/default/services/my-service', |
| 85 | + actionType: 'New', |
| 86 | + }; |
| 87 | + |
| 88 | + cy.mount( |
| 89 | + <CodePanel |
| 90 | + code={code} |
| 91 | + language={language} |
| 92 | + withAction={true} |
| 93 | + link={link} |
| 94 | + />, |
| 95 | + ); |
| 96 | + |
| 97 | + cy.get('.action-button') |
| 98 | + .eq(1) |
| 99 | + .should('exist'); |
| 100 | + cy.get('.action-button') |
| 101 | + .eq(1) |
| 102 | + .should('have.attr', 'icon', 'sys-add'); |
| 103 | + cy.get('.action-button') |
| 104 | + .eq(1) |
| 105 | + .should('contain.text', t('common.buttons.place')); |
| 106 | + }); |
| 107 | + |
| 108 | + it('renders the place button with correct attributes for cluster-level resources', () => { |
| 109 | + const code = |
| 110 | + 'apiVersion: rbac.authorization.k8s.io/v1\nkind: ClusterRole\nmetadata:\n name: my-role'; |
| 111 | + const language = 'yaml'; |
| 112 | + const link = { |
| 113 | + name: 'my-role', |
| 114 | + address: 'clusterroles/my-role', |
| 115 | + actionType: 'Update', |
| 116 | + }; |
| 117 | + |
| 118 | + cy.mount( |
| 119 | + <CodePanel |
| 120 | + code={code} |
| 121 | + language={language} |
| 122 | + withAction={true} |
| 123 | + link={link} |
| 124 | + />, |
| 125 | + ); |
| 126 | + |
| 127 | + cy.get('.action-button') |
| 128 | + .eq(1) |
| 129 | + .should('exist'); |
| 130 | + cy.get('.action-button') |
| 131 | + .eq(1) |
| 132 | + .should('have.attr', 'icon', 'sys-add'); |
| 133 | + cy.get('.action-button') |
| 134 | + .eq(1) |
| 135 | + .should('contain.text', t('common.buttons.place')); |
| 136 | + }); |
| 137 | + |
| 138 | + it('handles long code samples with proper wrapping', () => { |
| 139 | + const longCode = |
| 140 | + 'const veryLongVariableName = "This is an extremely long string that should trigger the line wrapping feature of the syntax highlighter component and test if it works properly in all cases, even with very long continuous text without natural breaking points";'; |
| 141 | + const language = 'javascript'; |
| 142 | + |
| 143 | + cy.mount(<CodePanel code={longCode} language={language} />); |
| 144 | + |
| 145 | + cy.get('pre').should('exist'); |
| 146 | + cy.get('code').should('be.visible'); |
| 147 | + // Visual check for wrapping will be done in UI |
| 148 | + }); |
| 149 | + |
| 150 | + it('renders correctly with different languages', () => { |
| 151 | + const languages = ['javascript', 'python', 'yaml', 'json', 'bash']; |
| 152 | + |
| 153 | + languages.forEach(lang => { |
| 154 | + const code = `// Sample ${lang} code`; |
| 155 | + cy.mount(<CodePanel code={code} language={lang} />); |
| 156 | + |
| 157 | + cy.get('ui5-title').should('contain.text', lang); |
| 158 | + cy.get('code').should('contain.text', code); |
| 159 | + cy.get('ui5-panel').should('exist'); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + it('handles empty code string gracefully', () => { |
| 164 | + cy.mount(<CodePanel code="" language="javascript" />); |
| 165 | + |
| 166 | + cy.get('.code-panel').should('exist'); |
| 167 | + cy.get('pre') |
| 168 | + .find('code') |
| 169 | + .should('exist'); |
| 170 | + }); |
| 171 | + |
| 172 | + it('renders correctly with Update action type', () => { |
| 173 | + const code = 'apiVersion: v1\nkind: Service\nmetadata:\n name: my-service'; |
| 174 | + const language = 'yaml'; |
| 175 | + const link = { |
| 176 | + name: 'my-service', |
| 177 | + address: 'namespaces/default/services/my-service', |
| 178 | + actionType: 'Update', |
| 179 | + }; |
| 180 | + |
| 181 | + cy.mount( |
| 182 | + <CodePanel |
| 183 | + code={code} |
| 184 | + language={language} |
| 185 | + withAction={true} |
| 186 | + link={link} |
| 187 | + />, |
| 188 | + ); |
| 189 | + |
| 190 | + cy.get('.action-button') |
| 191 | + .eq(1) |
| 192 | + .should('exist'); |
| 193 | + }); |
| 194 | + |
| 195 | + it('renders panel header with correct title', () => { |
| 196 | + const code = 'const hello = "world";'; |
| 197 | + const language = 'javascript'; |
| 198 | + |
| 199 | + cy.mount(<CodePanel code={code} language={language} />); |
| 200 | + |
| 201 | + cy.get('ui5-title').should('contain.text', language); |
| 202 | + cy.get('ui5-title').should('have.attr', 'level', 'H6'); |
| 203 | + }); |
| 204 | +}); |
0 commit comments