diff --git a/cypress/integration/select-multiple.spec.js b/cypress/integration/select-multiple.spec.js index c6bdbff..67123f0 100644 --- a/cypress/integration/select-multiple.spec.js +++ b/cypress/integration/select-multiple.spec.js @@ -414,5 +414,48 @@ describe('Choices - select multiple', () => { }); }); }); + + describe('remote data', () => { + beforeEach(() => { + cy.reload(true); + }); + + describe('when loading data', () => { + it('shows a loading message as a placeholder', () => { + cy.get('[data-test-hook=remote-data]') + .find('.choices__input--cloned') + .should('have.attr', 'placeholder', 'Loading...'); + }); + + describe('opening the dropdown', () => { + it('displays "no choices to choose" prompt', () => { + cy.get('[data-test-hook=remote-data]').click(); + cy.get('[data-test-hook=remote-data]') + .find('.choices__list--dropdown') + .should('be.visible') + .should($dropdown => { + const dropdownText = $dropdown.text().trim(); + expect(dropdownText).to.equal('No choices to choose from'); + }); + }); + }); + }); + + describe('when data has loaded', () => { + describe('opening the dropdown', () => { + it('displays the loaded data', () => { + cy.wait(2000); + cy.get('[data-test-hook=remote-data]') + .find('.choices__list--dropdown .choices__list') + .children() + .should('have.length', 50) + .each(($choice, index) => { + expect($choice.text().trim()).to.equal(`Label ${index + 1}`); + expect($choice.data('value')).to.equal(`Value ${index + 1}`); + }); + }); + }); + }); + }); }); }); diff --git a/cypress/integration/select-one.spec.js b/cypress/integration/select-one.spec.js index 7dbb77f..edaed62 100644 --- a/cypress/integration/select-one.spec.js +++ b/cypress/integration/select-one.spec.js @@ -351,5 +351,53 @@ describe('Choices - select one', () => { }); }); }); + + describe('remote data', () => { + beforeEach(() => { + cy.reload(true); + }); + + describe('when loading data', () => { + it('shows a loading message as a placeholder', () => { + cy.get('[data-test-hook=remote-data]') + .find('.choices__list--single') + .children() + .first() + .should('have.class', 'choices__placeholder') + .and($placeholder => { + expect($placeholder).to.contain('Loading...'); + }); + }); + + describe('opening the dropdown', () => { + it('displays "no choices to choose" prompt', () => { + cy.get('[data-test-hook=remote-data]').click(); + cy.get('[data-test-hook=remote-data]') + .find('.choices__list--dropdown') + .should('be.visible') + .should($dropdown => { + const dropdownText = $dropdown.text().trim(); + expect(dropdownText).to.equal('No choices to choose from'); + }); + }); + }); + }); + + describe('when data has loaded', () => { + describe('opening the dropdown', () => { + it('displays the loaded data', () => { + cy.wait(2000); + cy.get('[data-test-hook=remote-data]') + .find('.choices__list--dropdown .choices__list') + .children() + .should('have.length', 50) + .each(($choice, index) => { + expect($choice.text().trim()).to.equal(`Label ${index + 1}`); + expect($choice.data('value')).to.equal(`Value ${index + 1}`); + }); + }); + }); + }); + }); }); }); diff --git a/public/test/select-multiple.html b/public/test/select-multiple.html index 73a5ebc..c9e50b5 100644 --- a/public/test/select-multiple.html +++ b/public/test/select-multiple.html @@ -106,6 +106,11 @@ + +
+ + +
diff --git a/public/test/select-one.html b/public/test/select-one.html index f823724..062d174 100644 --- a/public/test/select-one.html +++ b/public/test/select-one.html @@ -94,6 +94,11 @@ + +
+ + +
diff --git a/server.js b/server.js index 6d8db72..dc54f77 100644 --- a/server.js +++ b/server.js @@ -26,6 +26,22 @@ if (process.env.NODE_ENV !== 'production') { ); app.use(webpackHotMiddleware(compiler)); + + app.get('/data', (req, res) => { + // prevent endpoint from being cached + res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate'); + res.header('Expires', '-1'); + res.header('Pragma', 'no-cache'); + + const fakeData = [...new Array(50)].map((_, index) => ({ + label: `Label ${index + 1}`, + value: `Value ${index + 1}`, + })); + + setTimeout(() => { + res.status(200).send(fakeData); + }, 2000); + }); } app.use(express.static(DIST_DIR));