|
| 1 | +import { vi, test, describe, expect } from 'vitest' |
| 2 | +import { $ } from '@wdio/globals' |
| 3 | + |
| 4 | +import { getExpectMessage, getReceived } from '../../__fixtures__/utils.js' |
| 5 | +import { toBeDisplayed } from '../../../src/matchers/element/toBeDisplayed.js' |
| 6 | + |
| 7 | +vi.mock('@wdio/globals') |
| 8 | + |
| 9 | +describe('toBeDisplayed', () => { |
| 10 | + /** |
| 11 | + * result is inverted for toBeDisplayed because it inverts isEnabled result |
| 12 | + * `!await el.isEnabled()` |
| 13 | + */ |
| 14 | + test('wait for success', async () => { |
| 15 | + const el: any = await $('sel') |
| 16 | + el._attempts = 2 |
| 17 | + el._value = function (): boolean { |
| 18 | + if (this._attempts > 0) { |
| 19 | + this._attempts-- |
| 20 | + return false |
| 21 | + } |
| 22 | + return true |
| 23 | + } |
| 24 | + |
| 25 | + const beforeAssertion = vi.fn() |
| 26 | + const afterAssertion = vi.fn() |
| 27 | + const result = await toBeDisplayed.call({}, el, {}, { beforeAssertion, afterAssertion }) |
| 28 | + expect(result.pass).toBe(true) |
| 29 | + expect(el._attempts).toBe(0) |
| 30 | + expect(beforeAssertion).toBeCalledWith({ |
| 31 | + matcherName: 'toBeDisplayed', |
| 32 | + options: { beforeAssertion, afterAssertion } |
| 33 | + }) |
| 34 | + expect(afterAssertion).toBeCalledWith({ |
| 35 | + matcherName: 'toBeDisplayed', |
| 36 | + options: { beforeAssertion, afterAssertion }, |
| 37 | + result |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + test('wait but failure', async () => { |
| 42 | + const el: any = await $('sel') |
| 43 | + el._value = function (): boolean { |
| 44 | + throw new Error('some error') |
| 45 | + } |
| 46 | + |
| 47 | + await expect(() => toBeDisplayed.call({}, el)) |
| 48 | + .rejects.toThrow('some error') |
| 49 | + }) |
| 50 | + |
| 51 | + test('success on the first attempt', async () => { |
| 52 | + const el: any = await $('sel') |
| 53 | + el._attempts = 0 |
| 54 | + el._value = function (): boolean { |
| 55 | + this._attempts++ |
| 56 | + return true |
| 57 | + } |
| 58 | + |
| 59 | + const result = await toBeDisplayed.call({}, el) |
| 60 | + expect(result.pass).toBe(true) |
| 61 | + expect(el._attempts).toBe(1) |
| 62 | + }) |
| 63 | + |
| 64 | + test('no wait - failure', async () => { |
| 65 | + const el: any = await $('sel') |
| 66 | + el._attempts = 0 |
| 67 | + el._value = function (): boolean { |
| 68 | + this._attempts++ |
| 69 | + return false |
| 70 | + } |
| 71 | + |
| 72 | + const result = await toBeDisplayed.call({}, el, {}, { wait: 0 }) |
| 73 | + expect(result.pass).toBe(false) |
| 74 | + expect(el._attempts).toBe(1) |
| 75 | + }) |
| 76 | + |
| 77 | + test('no wait - success', async () => { |
| 78 | + const el: any = await $('sel') |
| 79 | + el._attempts = 0 |
| 80 | + el._value = function (): boolean { |
| 81 | + this._attempts++ |
| 82 | + return true |
| 83 | + } |
| 84 | + |
| 85 | + const result = await toBeDisplayed.call({}, el, {}, { wait: 0 }) |
| 86 | + expect(result.pass).toBe(true) |
| 87 | + expect(el._attempts).toBe(1) |
| 88 | + }) |
| 89 | + |
| 90 | + test('not - failure', async () => { |
| 91 | + const el: any = await $('sel') |
| 92 | + el._value = function (): boolean { |
| 93 | + return true |
| 94 | + } |
| 95 | + const result = await toBeDisplayed.call({ isNot: true }, el, {}, { wait: 0 }) |
| 96 | + const received = getReceived(result.message()) |
| 97 | + |
| 98 | + expect(received).not.toContain('not') |
| 99 | + expect(result.pass).toBe(true) |
| 100 | + }) |
| 101 | + |
| 102 | + test('not - success', async () => { |
| 103 | + const el: any = await $('sel') |
| 104 | + el._value = function (): boolean { |
| 105 | + return false |
| 106 | + } |
| 107 | + const result = await toBeDisplayed.call({ isNot: true }, el, {}, { wait: 0 }) |
| 108 | + const received = getReceived(result.message()) |
| 109 | + |
| 110 | + expect(received).toContain('not') |
| 111 | + expect(result.pass).toBe(false) |
| 112 | + }) |
| 113 | + |
| 114 | + test('not - failure (with wait)', async () => { |
| 115 | + const el: any = await $('sel') |
| 116 | + el._value = function (): boolean { |
| 117 | + return true |
| 118 | + } |
| 119 | + const result = await toBeDisplayed.call({ isNot: true }, el, {}, { wait: 1 }) |
| 120 | + const received = getReceived(result.message()) |
| 121 | + |
| 122 | + expect(received).not.toContain('not') |
| 123 | + expect(result.pass).toBe(true) |
| 124 | + }) |
| 125 | + |
| 126 | + test('not - success (with wait)', async () => { |
| 127 | + const el: any = await $('sel') |
| 128 | + el._value = function (): boolean { |
| 129 | + return false |
| 130 | + } |
| 131 | + const result = await toBeDisplayed.call({ isNot: true }, el, {}, { wait: 1 }) |
| 132 | + const received = getReceived(result.message()) |
| 133 | + |
| 134 | + expect(received).toContain('not') |
| 135 | + expect(result.pass).toBe(false) |
| 136 | + }) |
| 137 | + |
| 138 | + test('message', async () => { |
| 139 | + const el: any = await $('sel') |
| 140 | + el._value = function (): boolean { |
| 141 | + return false |
| 142 | + } |
| 143 | + const result = await toBeDisplayed.call({}, el) |
| 144 | + expect(getExpectMessage(result.message())).toContain('to be displayed') |
| 145 | + }) |
| 146 | +}) |
0 commit comments