|
| 1 | +import { Ensure, equals, isFalse, isTrue } from '@serenity-js/assertions'; |
| 2 | +import { notes, TakeNotes } from '@serenity-js/core'; |
| 3 | +import { Attribute, By, PageElement } from '@serenity-js/web'; |
| 4 | + |
| 5 | +import { default as PasswordComponent } from './Password.js'; |
| 6 | +import { Password } from './Password.serenity.js'; |
| 7 | + |
| 8 | +import { describe, it } from '../test-api'; |
| 9 | + |
| 10 | +describe('Password', () => { |
| 11 | + |
| 12 | + it('hides the password by default', async ({ mount, actor }) => { |
| 13 | + const passwordField = PageElement.from(await mount(PasswordComponent, { |
| 14 | + props: { |
| 15 | + placeholder: 'Enter password', |
| 16 | + }, |
| 17 | + })).describedAs('password field') |
| 18 | + |
| 19 | + await actor.attemptsTo( |
| 20 | + Ensure.that( |
| 21 | + Password.isVisible().in(passwordField), |
| 22 | + isFalse() |
| 23 | + ), |
| 24 | + ) |
| 25 | + }); |
| 26 | + |
| 27 | + it('reveals the password when the eye icon is clicked', async ({ mount, actor }) => { |
| 28 | + const passwordField = PageElement.from(await mount(PasswordComponent, { |
| 29 | + props: { |
| 30 | + placeholder: 'Enter password', |
| 31 | + }, |
| 32 | + })).describedAs('password field') |
| 33 | + |
| 34 | + await actor.attemptsTo( |
| 35 | + Ensure.that(Password.isVisible().in(passwordField), isFalse()), |
| 36 | + |
| 37 | + Password.reveal(passwordField), |
| 38 | + |
| 39 | + Ensure.that(Password.isVisible().in(passwordField), isTrue()), |
| 40 | + ) |
| 41 | + }); |
| 42 | + |
| 43 | + it('hides the password when the eye icon is clicked again', async ({ mount, actor }) => { |
| 44 | + const passwordField = PageElement.from(await mount(PasswordComponent, { |
| 45 | + props: { |
| 46 | + placeholder: 'Enter password', |
| 47 | + }, |
| 48 | + })).describedAs('password field') |
| 49 | + |
| 50 | + await actor.attemptsTo( |
| 51 | + Password.reveal(passwordField), |
| 52 | + Ensure.that(Password.isVisible().in(passwordField), isTrue()), |
| 53 | + |
| 54 | + Password.hide(passwordField), |
| 55 | + |
| 56 | + Ensure.that(Password.isVisible().in(passwordField), isFalse()), |
| 57 | + ) |
| 58 | + }); |
| 59 | + |
| 60 | + it('allows typing a password', async ({ mount, actor }) => { |
| 61 | + const testPassword = 'SecurePassword123!'; |
| 62 | + |
| 63 | + const passwordField = PageElement.from(await mount(PasswordComponent, { |
| 64 | + props: { |
| 65 | + placeholder: 'Enter password', |
| 66 | + }, |
| 67 | + })).describedAs('password field') |
| 68 | + |
| 69 | + await actor.attemptsTo( |
| 70 | + Password.type(testPassword).into(passwordField), |
| 71 | + |
| 72 | + Ensure.that( |
| 73 | + Password.value().of(passwordField), |
| 74 | + equals(testPassword) |
| 75 | + ), |
| 76 | + ) |
| 77 | + }); |
| 78 | + |
| 79 | + |
| 80 | + it('triggers visibilitychange event when toggling', async ({ mount, actor }) => { |
| 81 | + function spy<Arguments extends any[]>(name: string): (spyArguments: Arguments) => void { |
| 82 | + return (spyArguments: Arguments) => TakeNotes.as(actor).notepad.set(name, spyArguments); |
| 83 | + } |
| 84 | + |
| 85 | + const passwordField = PageElement.from(await mount(PasswordComponent, { |
| 86 | + props: { |
| 87 | + placeholder: 'Enter password', |
| 88 | + }, |
| 89 | + on: { |
| 90 | + visibilitychange: spy('visibilityState'), |
| 91 | + } |
| 92 | + })).describedAs('password field') |
| 93 | + |
| 94 | + await actor.attemptsTo( |
| 95 | + Password.toggle(passwordField), |
| 96 | + |
| 97 | + Ensure.eventually(notes().get('visibilityState'), isTrue()), |
| 98 | + |
| 99 | + Password.toggle(passwordField), |
| 100 | + |
| 101 | + Ensure.eventually(notes().get('visibilityState'), isFalse()), |
| 102 | + ) |
| 103 | + }); |
| 104 | + |
| 105 | + it('displays the placeholder text', async ({ mount, actor }) => { |
| 106 | + const placeholder = 'Enter your secure password'; |
| 107 | + |
| 108 | + const passwordField = PageElement.from(await mount(PasswordComponent, { |
| 109 | + props: { |
| 110 | + placeholder, |
| 111 | + }, |
| 112 | + })).describedAs('password field') |
| 113 | + |
| 114 | + await actor.attemptsTo( |
| 115 | + Ensure.that( |
| 116 | + Attribute.called('placeholder') |
| 117 | + .of(PageElement.located(By.deepCss('.password-input')).of(passwordField)), |
| 118 | + equals(placeholder) |
| 119 | + ), |
| 120 | + ) |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
0 commit comments