|
| 1 | +import {render} from './helpers/test-utils' |
| 2 | + |
| 3 | +/* eslint-disable max-statements */ |
| 4 | +test('.toContainHTML', () => { |
| 5 | + const {queryByTestId} = render(` |
| 6 | + <span data-testid="grandparent"> |
| 7 | + <span data-testid="parent"> |
| 8 | + <span data-testid="child"></span> |
| 9 | + </span> |
| 10 | + <svg data-testid="svg-element"></svg> |
| 11 | + </span> |
| 12 | + `) |
| 13 | + |
| 14 | + const grandparent = queryByTestId('grandparent') |
| 15 | + const parent = queryByTestId('parent') |
| 16 | + const child = queryByTestId('child') |
| 17 | + const nonExistantElement = queryByTestId('not-exists') |
| 18 | + const fakeElement = {thisIsNot: 'an html element'} |
| 19 | + const stringChildElement = '<span data-testid="child"></span>' |
| 20 | + const incorrectStringHtml = '<span data-testid="child"></div>' |
| 21 | + const nonExistantString = '<span> Does not exists </span>' |
| 22 | + const svgElement = queryByTestId('svg-element') |
| 23 | + |
| 24 | + expect(grandparent).toContainHTML(stringChildElement) |
| 25 | + expect(parent).toContainHTML(stringChildElement) |
| 26 | + expect(child).toContainHTML(stringChildElement) |
| 27 | + expect(grandparent).not.toContainHTML(nonExistantString) |
| 28 | + expect(parent).not.toContainHTML(nonExistantString) |
| 29 | + expect(child).not.toContainHTML(nonExistantString) |
| 30 | + expect(child).not.toContainHTML(nonExistantString) |
| 31 | + expect(grandparent).not.toContainHTML(incorrectStringHtml) |
| 32 | + expect(parent).not.toContainHTML(incorrectStringHtml) |
| 33 | + expect(child).not.toContainHTML(incorrectStringHtml) |
| 34 | + expect(child).not.toContainHTML(incorrectStringHtml) |
| 35 | + |
| 36 | + // negative test cases wrapped in throwError assertions for coverage. |
| 37 | + expect(() => |
| 38 | + expect(nonExistantElement).not.toContainHTML(stringChildElement), |
| 39 | + ).toThrowError() |
| 40 | + expect(() => |
| 41 | + expect(nonExistantElement).not.toContainHTML(nonExistantElement), |
| 42 | + ).toThrowError() |
| 43 | + expect(() => |
| 44 | + expect(stringChildElement).not.toContainHTML(fakeElement), |
| 45 | + ).toThrowError() |
| 46 | + expect(() => |
| 47 | + expect(svgElement).toContainHTML(stringChildElement), |
| 48 | + ).toThrowError() |
| 49 | + expect(() => |
| 50 | + expect(grandparent).not.toContainHTML(stringChildElement), |
| 51 | + ).toThrowError() |
| 52 | + expect(() => |
| 53 | + expect(parent).not.toContainHTML(stringChildElement), |
| 54 | + ).toThrowError() |
| 55 | + expect(() => |
| 56 | + expect(child).not.toContainHTML(stringChildElement), |
| 57 | + ).toThrowError() |
| 58 | + expect(() => |
| 59 | + expect(child).not.toContainHTML(stringChildElement), |
| 60 | + ).toThrowError() |
| 61 | + expect(() => expect(child).toContainHTML(nonExistantString)).toThrowError() |
| 62 | + expect(() => expect(parent).toContainHTML(nonExistantString)).toThrowError() |
| 63 | + expect(() => |
| 64 | + expect(grandparent).toContainHTML(nonExistantString), |
| 65 | + ).toThrowError() |
| 66 | + expect(() => expect(child).toContainHTML(nonExistantElement)).toThrowError() |
| 67 | + expect(() => expect(parent).toContainHTML(nonExistantElement)).toThrowError() |
| 68 | + expect(() => |
| 69 | + expect(grandparent).toContainHTML(nonExistantElement), |
| 70 | + ).toThrowError() |
| 71 | + expect(() => |
| 72 | + expect(nonExistantElement).toContainHTML(incorrectStringHtml), |
| 73 | + ).toThrowError() |
| 74 | + expect(() => |
| 75 | + expect(grandparent).toContainHTML(incorrectStringHtml), |
| 76 | + ).toThrowError() |
| 77 | + expect(() => expect(child).toContainHTML(incorrectStringHtml)).toThrowError() |
| 78 | + expect(() => expect(parent).toContainHTML(incorrectStringHtml)).toThrowError() |
| 79 | +}) |
0 commit comments