|
| 1 | +import { expect, Locator, Page } from '@playwright/test'; |
| 2 | +import Input from 'dss/component/input'; |
| 3 | +import { waitForResponse } from 'utils/test-utils'; |
| 4 | +import Modal from './modal'; |
| 5 | + |
| 6 | +interface InputData { |
| 7 | + value: string; |
| 8 | + selectCheckbox?: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +export default class SMID { |
| 12 | + private readonly modalComponent: Modal; |
| 13 | + |
| 14 | + constructor(private readonly page: Page, private readonly tissueIndex: number) { |
| 15 | + this.modalComponent = new Modal(this.page); |
| 16 | + } |
| 17 | + |
| 18 | + public async getValueAt(index: number): Promise<string> { |
| 19 | + const input = await this.getInputAt(index); |
| 20 | + return input.currentValue(); |
| 21 | + } |
| 22 | + |
| 23 | + public async fillInputs(inputData: (InputData | string)[]): Promise<void> { |
| 24 | + for (let i = 0; i < inputData.length; i++) { |
| 25 | + const field = inputData[i]; |
| 26 | + const value = typeof field === 'object' ? field.value : field; |
| 27 | + const selectCheckbox = typeof field === 'object' ? field.selectCheckbox : false; |
| 28 | + |
| 29 | + const isInputVisible = await this.isInputVisible(i); |
| 30 | + if (!isInputVisible) { |
| 31 | + await this.addField(); |
| 32 | + } |
| 33 | + await this.fillField(value, i); |
| 34 | + selectCheckbox && await this.selectCheckbox(i); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public async deleteInputAt(index: number): Promise<void> { |
| 39 | + const deleteBtnLocator = this.fields.nth(index).locator('//td/button'); |
| 40 | + await expect(deleteBtnLocator, `Delete button is not visible at index ${index}`).toBeVisible(); |
| 41 | + |
| 42 | + await deleteBtnLocator.click(); |
| 43 | + } |
| 44 | + |
| 45 | + public async close(): Promise<void> { |
| 46 | + await this.clickModalBtn('close'); |
| 47 | + } |
| 48 | + |
| 49 | + public async onlyKeepSelectedSMIDs(): Promise<void> { |
| 50 | + await this.clickModalBtn('Only keep selected SM-IDs'); |
| 51 | + await waitForResponse(this.page, { uri: 'patch' }); |
| 52 | + } |
| 53 | + |
| 54 | + /* Helper Functions */ |
| 55 | + private async isInputVisible(index: number): Promise<boolean> { |
| 56 | + return this.fields.nth(index).isVisible(); |
| 57 | + } |
| 58 | + |
| 59 | + private async addField(): Promise<void> { |
| 60 | + await this.addBtn.click(); |
| 61 | + } |
| 62 | + |
| 63 | + private async fillField(value: string, index: number): Promise<void> { |
| 64 | + const fieldInput = await this.getInputAt(index); |
| 65 | + const currentValue = await fieldInput.currentValue(); |
| 66 | + if (currentValue.trim() !== value) { |
| 67 | + await fieldInput.fillSimple(value); |
| 68 | + await waitForResponse(this.page, { uri: 'patch' }); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private async selectCheckbox(index: number): Promise<void> { |
| 73 | + const checkboxLocator = this.fields.nth(index).locator('//td/mat-checkbox'); |
| 74 | + await expect(checkboxLocator, `Checkbox is not visible at index ${index}`).toBeVisible(); |
| 75 | + await checkboxLocator.click(); |
| 76 | + } |
| 77 | + |
| 78 | + private async getInputAt(index: number): Promise<Input> { |
| 79 | + const fieldLocator = this.fields.nth(index).locator('//td/md-input-container'); |
| 80 | + await expect(fieldLocator, `Input field is not visible at index ${index}`).toBeVisible(); |
| 81 | + return new Input(this.page, { root: fieldLocator }); |
| 82 | + } |
| 83 | + |
| 84 | + private async clickModalBtn(label: 'Only keep selected SM-IDs' | 'close'): Promise<void> { |
| 85 | + await this.modalComponent.getButton({ label }).click(); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + /* Locators */ |
| 90 | + private get modalBody(): Locator { |
| 91 | + return this.modalComponent.bodyLocator(); |
| 92 | + } |
| 93 | + |
| 94 | + private get modal(): Locator { |
| 95 | + return this.page.locator(`(//app-tissue)[${this.tissueIndex + 1}]/app-modal`); |
| 96 | + } |
| 97 | + |
| 98 | + private get fields(): Locator { |
| 99 | + return this.modalBody.locator('//table/tr[td[md-input-container]]'); |
| 100 | + } |
| 101 | + |
| 102 | + private get addBtn(): Locator { |
| 103 | + return this.modalBody.locator(`//div[@class='app-modal-body']/button`); |
| 104 | + } |
| 105 | +} |
0 commit comments