|
| 1 | +describe('find* dom-testing-library commands', () => { |
| 2 | + beforeEach(() => { |
| 3 | + cy.visit('/') |
| 4 | + }) |
| 5 | + |
| 6 | + // Test each of the types of queries: LabelText, PlaceholderText, Text, DisplayValue, AltText, Title, Role, TestId |
| 7 | + |
| 8 | + it('findByLabelText', () => { |
| 9 | + cy.findByLabelText('Label 1') |
| 10 | + .click() |
| 11 | + .type('Hello Input Labelled By Id') |
| 12 | + }) |
| 13 | + |
| 14 | + it('findAllByLabelText', () => { |
| 15 | + cy.findAllByLabelText(/^Label \d$/).should('have.length', 2) |
| 16 | + }) |
| 17 | + |
| 18 | + it('findByPlaceholderText', () => { |
| 19 | + cy.findByPlaceholderText('Input 1') |
| 20 | + .click() |
| 21 | + .type('Hello Placeholder') |
| 22 | + }) |
| 23 | + |
| 24 | + it('findAllByPlaceholderText', () => { |
| 25 | + cy.findAllByPlaceholderText(/^Input \d$/).should('have.length', 2) |
| 26 | + }) |
| 27 | + |
| 28 | + it('findByText', () => { |
| 29 | + cy.findByText('Button Text 1') |
| 30 | + .click() |
| 31 | + .should('contain', 'Button Clicked') |
| 32 | + }) |
| 33 | + |
| 34 | + it('findAllByText', () => { |
| 35 | + cy.findAllByText(/^Button Text \d$/) |
| 36 | + .should('have.length', 2) |
| 37 | + .click({ multiple: true }) |
| 38 | + .should('contain', 'Button Clicked') |
| 39 | + }) |
| 40 | + |
| 41 | + it('findByDisplayValue', () => { |
| 42 | + cy.findByDisplayValue('Display Value 1') |
| 43 | + .click() |
| 44 | + .clear() |
| 45 | + .type('Some new text') |
| 46 | + }) |
| 47 | + |
| 48 | + it('findAllByDisplayValue', () => { |
| 49 | + cy.findAllByDisplayValue(/^Display Value \d$/) |
| 50 | + .should('have.length', 2) |
| 51 | + }) |
| 52 | + |
| 53 | + it('findByAltText', () => { |
| 54 | + cy.findByAltText('Image Alt Text 1').click() |
| 55 | + }) |
| 56 | + |
| 57 | + it('findAllByAltText', () => { |
| 58 | + cy.findAllByAltText(/^Image Alt Text \d$/).should('have.length', 2) |
| 59 | + }) |
| 60 | + |
| 61 | + it('findByTitle', () => { |
| 62 | + cy.findByTitle('Title 1').click() |
| 63 | + }) |
| 64 | + |
| 65 | + it('findAllByTitle', () => { |
| 66 | + cy.findAllByTitle(/^Title \d$/).should('have.length', 2) |
| 67 | + }) |
| 68 | + |
| 69 | + it('findByRole', () => { |
| 70 | + cy.findByRole('dialog').click() |
| 71 | + }) |
| 72 | + |
| 73 | + it('findAllByRole', () => { |
| 74 | + cy.findAllByRole(/^dialog/).should('have.length', 2) |
| 75 | + }) |
| 76 | + |
| 77 | + it('findByTestId', () => { |
| 78 | + cy.findByTestId('image-with-random-alt-tag-1').click() |
| 79 | + }) |
| 80 | + |
| 81 | + it('findAllByTestId', () => { |
| 82 | + cy.findAllByTestId(/^image-with-random-alt-tag-\d$/).should('have.length', 2) |
| 83 | + }) |
| 84 | + |
| 85 | + /* Test the behaviour around these queries */ |
| 86 | + |
| 87 | + it('findByText with should(\'not.exist\')', () => { |
| 88 | + cy.findAllByText(/^Button Text \d$/).should('exist') |
| 89 | + cy.findByText('Non-existing Button Text', {timeout: 100}).should('not.exist') |
| 90 | + }) |
| 91 | + |
| 92 | + it('findByText within', () => { |
| 93 | + cy.get('#nested').within(() => { |
| 94 | + cy.findByText('Button Text 2').click() |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('findByText in container', () => { |
| 99 | + return cy.get('#nested') |
| 100 | + .then(subject => { |
| 101 | + cy.findByText(/^Button Text/, {container: subject}).click() |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + it('findByText works when another page loads', () => { |
| 106 | + cy.findByText('Next Page').click() |
| 107 | + cy.findByText('New Page Loaded').should('exist') |
| 108 | + }) |
| 109 | + |
| 110 | + it('findByText should error if no elements are found', () => { |
| 111 | + const regex = /Supercalifragilistic/ |
| 112 | + const errorMessage = `Timed out retrying: Expected to find element: 'findByText(${regex})', but never found it.` |
| 113 | + cy.on('fail', err => { |
| 114 | + expect(err.message).to.eq(errorMessage) |
| 115 | + }) |
| 116 | + |
| 117 | + cy.findByText(regex, {timeout: 100}) // Doesn't explicitly need .should('exist') if it's the last element? |
| 118 | + }) |
| 119 | + |
| 120 | + it('findByText finding multiple items should error', () => { |
| 121 | + const errorMessage = `Found multiple elements with the text: /^Button Text/i\n\n(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).` |
| 122 | + cy.on('fail', err => { |
| 123 | + expect(err.message).to.eq(errorMessage) |
| 124 | + }) |
| 125 | + |
| 126 | + cy.findByText(/^Button Text/i) |
| 127 | + }) |
| 128 | +}) |
| 129 | + |
| 130 | +/* global cy */ |
0 commit comments