diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..d805659f --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +# http://editorconfig.org +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 2 +insert_final_newline = false +trim_trailing_whitespace = true diff --git a/.eslintrc b/.eslintrc index 1f883e38..2afe7349 100644 --- a/.eslintrc +++ b/.eslintrc @@ -13,6 +13,8 @@ "Sortable": true }, "rules": { + "cypress/no-unnecessary-waiting": "off", + "cypress/unsafe-to-chain-command": "off", "no-prototype-builtins": [0] } } \ No newline at end of file diff --git a/cypress/integration/example-0070-plugin-state.spec.js b/cypress/integration/example-0070-plugin-state.spec.js new file mode 100644 index 00000000..96ccf853 --- /dev/null +++ b/cypress/integration/example-0070-plugin-state.spec.js @@ -0,0 +1,114 @@ + +describe('Example 0070 - Grid State using Local Storage', () => { + const originalTitles = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']; + + beforeEach(() => { + cy.restoreLocalStorage(); + }); + + afterEach(() => { + cy.saveLocalStorage(); + }); + + it('should display Example title', () => { + cy.visit(`${Cypress.config('baseExampleUrl')}/example-0070-plugin-state.html`); + cy.get('h2 + p').should('contain', 'Slick.State'); + + cy.clearLocalStorage(); + cy.get('[data-test="clear-storage"]').click(); + }); + + it('should have exact Column Titles in the grid', () => { + cy.get('.slick-header-columns') + .children() + .each(($child, index) => expect($child.text()).to.eq(originalTitles[index])); + }); + + it('should sort ascending column "D"', () => { + cy.get('.slick-header-columns .slick-header-column:nth(3)') + .click({ force: true }); + + cy.get('.slick-header-columns') + .children('.slick-header-column:nth(3)') + .find('.slick-sort-indicator.slick-sort-indicator-asc') + .should('be.visible'); + }); + + it('should drag column "A" to be after column "C" in the grid', () => { + const expectedTitles = ['A', 'C', 'D', 'B', 'E', 'F', 'G', 'H', 'I', 'J']; + + cy.get('.slick-header-columns') + .children('.slick-header-column:nth(1)') + .contains('B') + .drag('.slick-header-column:nth(3)'); + + cy.get('.slick-header-column:nth(3)').contains('B'); + + cy.get('.slick-header-columns') + .children() + .each(($child, index) => expect($child.text()).to.eq(expectedTitles[index])); + }); + + it('should resize 1st column and make it wider', () => { + cy.get('.slick-header-columns') + .children('.slick-header-column:nth(1)') + .should('contain', 'C'); + + cy.get('.slick-resizable-handle:nth(1)') + .trigger('mousedown', { which: 1, force: true }) + .trigger('mousemove', 'bottomRight'); + + cy.get('.slick-header-column:nth(2)') + .trigger('mousemove', 'bottomRight') + .trigger('mouseup', 'bottomRight', { which: 1, force: true }); + + cy.get('.slick-header-column:nth(1)') + .should(($el) => { + const expectedWidth = 170; // calculate with a calculated width including a (+/-)5px precision + expect($el.width()).greaterThan(expectedWidth - 5); + expect($el.width()).lessThan(expectedWidth + 5); + }); + }); + + it('should reload the page', () => { + cy.reload().wait(50); + }); + + it('should reload page and expect same columns orders, 2nd column wider and 3rd column sorted ascending', () => { + const expectedTitles = ['A', 'C', 'D', 'B', 'E', 'F', 'G', 'H', 'I', 'J']; + + cy.get('.slick-header-columns') + .children() + .each(($child, index) => expect($child.text()).to.eq(expectedTitles[index])); + + cy.get('.slick-header-column:nth(1)') + .should(($el) => { + const expectedWidth = 170; // calculate with a calculated width including a (+/-)5px precision + expect($el.width()).greaterThan(expectedWidth - 5); + expect($el.width()).lessThan(expectedWidth + 5); + }); + + cy.get('.slick-header-columns') + .children('.slick-header-column:nth(2)') + .find('.slick-sort-indicator.slick-sort-indicator-asc') + .should('be.visible'); + }); + + it('should clear local storage & reload the page', () => { + cy.clearLocalStorage(); + cy.get('[data-test="clear-storage"]').click(); + + cy.reload().wait(50); + }); + + it('should expect grid to be reset to default', () => { + cy.get('.slick-header-columns') + .children() + .each(($child, index) => expect($child.text()).to.eq(originalTitles[index])); + + cy.get('.slick-header-columns') + .children('.slick-header-column:nth(2)') + .find('.slick-sort-indicator.slick-sort-indicator-asc') + .should('not.exist'); + }); +}); \ No newline at end of file diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 51479eb1..07343c3e 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -35,4 +35,18 @@ Cypress.Commands.add("getCell", (row, col, viewport = 'topLeft', { parentSelecto const canvasSelectorY = position.y ? `.grid-canvas-${position.y}` : ''; return cy.get(`${parentSelector} ${canvasSelectorX}${canvasSelectorY} [style="top:${row * rowHeight}px"] > .slick-cell:nth(${col})`); -}) \ No newline at end of file +}); + +let LOCAL_STORAGE_MEMORY = {}; + +Cypress.Commands.add('saveLocalStorage', () => { + Object.keys(localStorage).forEach(key => { + LOCAL_STORAGE_MEMORY[key] = localStorage[key]; + }); +}); + +Cypress.Commands.add('restoreLocalStorage', () => { + Object.keys(LOCAL_STORAGE_MEMORY).forEach(key => { + localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]); + }); +}); \ No newline at end of file diff --git a/examples/example-0070-plugin-state.html b/examples/example-0070-plugin-state.html index 8c7e6406..440ec368 100644 --- a/examples/example-0070-plugin-state.html +++ b/examples/example-0070-plugin-state.html @@ -2,6 +2,7 @@
+