|
| 1 | +/** |
| 2 | + * Copyright (C) 2025 Arthit Suriyawongkul |
| 3 | + * See the AUTHORS file at the top-level directory of this distribution |
| 4 | + * License: GNU Affero General Public License version 3, or any later version |
| 5 | + * See top-level LICENSE file for more information |
| 6 | + */ |
| 7 | + |
| 8 | +/* |
| 9 | + * Tests the user interface. |
| 10 | + */ |
| 11 | + |
| 12 | +"use strict"; |
| 13 | + |
| 14 | +describe('License input field', function () { |
| 15 | + beforeEach(function () { |
| 16 | + cy.window().then((win) => { |
| 17 | + win.sessionStorage.clear(); |
| 18 | + win.document.getElementById('selected-licenses').innerHTML = ''; |
| 19 | + }) |
| 20 | + cy.visit('./index.html'); |
| 21 | + // Wait for the license list to be loaded asynchronously before running |
| 22 | + // license field-related tests. This prevents races where tests trigger |
| 23 | + // input/change events before SPDX_LICENSE_IDS is available. |
| 24 | + cy.window().its('SPDX_LICENSE_IDS.length').should('be.gt', 0); |
| 25 | + cy.get('#license').should('exist'); |
| 26 | + cy.get('#selected-licenses').should('exist'); |
| 27 | + cy.get('#selected-licenses').children().should('have.length', 0); |
| 28 | + }); |
| 29 | + |
| 30 | + it('can add a license by typing', function () { |
| 31 | + cy.get('#license').type('MIT{enter}'); |
| 32 | + cy.get('#selected-licenses .license-id').should('contain', 'MIT'); |
| 33 | + cy.get('#license').should('have.value', ''); |
| 34 | + }); |
| 35 | + |
| 36 | + it('can add a license by typing non-strictly with spaces', function () { |
| 37 | + // " MIT " -> "MIT" |
| 38 | + cy.get('#license').type(' MIT {enter}'); |
| 39 | + cy.get('#selected-licenses .license-id').should('contain', 'MIT'); |
| 40 | + cy.get('#license').should('have.value', ''); |
| 41 | + }); |
| 42 | + |
| 43 | + it('can add a license by typing non-strictly to original casing', function () { |
| 44 | + // "mit" -> "MIT" |
| 45 | + cy.get('#license').type('mit{enter}'); |
| 46 | + cy.get('#selected-licenses .license-id').should('contain', 'MIT'); |
| 47 | + cy.get('#license').should('have.value', ''); |
| 48 | + }); |
| 49 | + |
| 50 | + it('can add a license by clicking on the pop-up list', function () { |
| 51 | + // inputType "insertReplacementText" typically appears when the browser |
| 52 | + // inserts/replaces the input value as a single operation |
| 53 | + // (for example when the user picks an item from a datalist with the |
| 54 | + // mouse or when autocompletion replaces the current text) |
| 55 | + cy.get('#license') |
| 56 | + .invoke('val', 'MIT') |
| 57 | + .trigger('input', { inputType: 'insertReplacementText' }); |
| 58 | + cy.get('#selected-licenses .license-id', { timeout: 5000 }) |
| 59 | + .should('contain', 'MIT'); |
| 60 | + cy.get('#license').should('have.value', ''); |
| 61 | + }); |
| 62 | + |
| 63 | + it('can add a license on "change" event (e.g., selecting via keyboard)', function () { |
| 64 | + cy.get('#license').invoke('val', 'MIT').trigger('change'); |
| 65 | + cy.get('#selected-licenses .license-id', { timeout: 5000 }) |
| 66 | + .should('contain', 'MIT'); |
| 67 | + cy.get('#license').should('have.value', ''); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should not insert a duplicated license', function () { |
| 71 | + cy.get('#license').type('MIT{enter}'); |
| 72 | + cy.get('#selected-licenses .license-id').should('have.length', 1); |
| 73 | + cy.get('#license').type('MIT{enter}'); |
| 74 | + cy.get('#selected-licenses .license-id').should('have.length', 1); |
| 75 | + cy.get('#license').should('have.value', ''); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should not insert an invalid license', function () { |
| 79 | + cy.get('#license').type('INVALID_LICENSE00{enter}'); |
| 80 | + cy.get('#selected-licenses .license-id').should('not.exist'); |
| 81 | + cy.get('#license:invalid').should('exist'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should not insert a shorter match while typing a longer id with same prefix', function () { |
| 85 | + // "MIT-0" vs "MIT" |
| 86 | + cy.get('#license').type('MIT-0{enter}'); |
| 87 | + cy.get('#selected-licenses .license-id').should('contain', 'MIT-0'); |
| 88 | + cy.get('#license').should('have.value', ''); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should not insert until user confirms with Enter', function () { |
| 92 | + // Type "MIT" but do not confirm with Enter yet |
| 93 | + cy.get('#license').type('MIT'); |
| 94 | + cy.get('#selected-licenses .license-id').should('not.exist'); |
| 95 | + // Press ArrowRight - which is not a confirmation |
| 96 | + cy.get('#license').trigger('keydown', { key: 'ArrowRight', code: 'ArrowRight', keyCode: 39, which: 39 }); |
| 97 | + cy.get('#selected-licenses .license-id').should('not.exist'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should not insert when edits produce a matching ID', function () { |
| 101 | + // Note the space after "MIT" |
| 102 | + cy.get('#license').type('MIT -0'); |
| 103 | + cy.get('#selected-licenses .license-id').should('not.exist'); |
| 104 | + // Remove the space, to produce "MIT-0" |
| 105 | + cy.get('#license').type('{leftarrow}{leftarrow}{backspace}'); |
| 106 | + cy.get('#selected-licenses .license-id').should('not.exist'); |
| 107 | + // Confirm with Enter |
| 108 | + cy.get('#license').type('{enter}'); |
| 109 | + cy.get('#selected-licenses .license-id').should('contain', 'MIT-0'); |
| 110 | + cy.get('#license').should('have.value', ''); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should insert when user confirms with Enter', function () { |
| 114 | + // Type "MIT" but do not confirm with Enter yet |
| 115 | + cy.get('#license').type('MIT'); |
| 116 | + cy.get('#selected-licenses .license-id').should('not.exist'); |
| 117 | + // Simulate Enter keydown to confirm |
| 118 | + cy.get('#license').trigger('keydown', { key: 'Enter', code: 'Enter', keyCode: 13, which: 13 }); |
| 119 | + cy.get('#selected-licenses .license-id').should('contain', 'MIT'); |
| 120 | + cy.get('#license').should('have.value', ''); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should insert when user press Tab', function () { |
| 124 | + // Type "MIT" but do not press Tab yet |
| 125 | + cy.get('#license').type('MIT'); |
| 126 | + cy.get('#selected-licenses .license-id').should('not.exist'); |
| 127 | + // Simulate Tab keydown to confirm |
| 128 | + cy.get('#license').trigger('keydown', { key: 'Tab', code: 'Tab', keyCode: 9, which: 9 }); |
| 129 | + cy.get('#selected-licenses .license-id').should('contain', 'MIT'); |
| 130 | + cy.get('#license').should('have.value', ''); |
| 131 | + }); |
| 132 | +}); |
0 commit comments