settings.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { expect, type Locator, type Page } from '@playwright/test'
  2. import { fillSettled, settle } from './hydration'
  3. /**
  4. * The workshop settings a spec has to move before it can assert anything: tax,
  5. * and the rules that decide an invoice number.
  6. *
  7. * These are one workshop's settings, shared by every test in the run, so a
  8. * spec that changes one puts it back at the end. Both pages are controlled
  9. * React forms, which is why the fields are typed into through `fillSettled`
  10. * rather than `fill`.
  11. */
  12. export interface TaxComponentSetup {
  13. name: string
  14. rate: number
  15. registrationNumber?: string
  16. }
  17. export interface TaxSetup {
  18. enabled: boolean
  19. rate?: number
  20. inclusive?: boolean
  21. label?: string
  22. /**
  23. * More than one tax on a document (Québec's GST and QST). Given, the split
  24. * switch is turned on and these rows replace the single rate; left out, the
  25. * switch is turned off and the workshop is back to one rate.
  26. */
  27. components?: TaxComponentSetup[]
  28. }
  29. /** Flips a switch to the wanted state, retried because a click before hydration does nothing. */
  30. async function setSwitch(toggle: Locator, on: boolean): Promise<void> {
  31. await expect(toggle).toBeVisible()
  32. await expect(async () => {
  33. if ((await toggle.getAttribute('aria-checked')) === String(on)) return
  34. await toggle.click()
  35. await expect(toggle).toHaveAttribute('aria-checked', String(on))
  36. }).toPass()
  37. }
  38. /** Settings → Tax, saved. */
  39. export async function setTax(page: Page, tax: TaxSetup): Promise<void> {
  40. await page.goto('/settings/tax')
  41. await setSwitch(page.getByRole('switch').first(), tax.enabled)
  42. if (tax.enabled) {
  43. await setSwitch(page.locator('#taxSplit'), Boolean(tax.components))
  44. if (tax.components) {
  45. const rows = page.getByTestId('tax-component-row')
  46. while ((await rows.count()) > tax.components.length) {
  47. await rows.last().getByRole('button', { name: 'Remove this tax' }).click()
  48. }
  49. while ((await rows.count()) < tax.components.length) {
  50. await page.getByRole('button', { name: 'Add a tax' }).click()
  51. }
  52. for (const [i, component] of tax.components.entries()) {
  53. await fillSettled(page.locator(`#taxComponentName-${i}`), component.name)
  54. await fillSettled(page.locator(`#taxComponentRate-${i}`), String(component.rate))
  55. await fillSettled(
  56. page.locator(`#taxComponentRegistration-${i}`),
  57. component.registrationNumber ?? ''
  58. )
  59. }
  60. } else {
  61. await page.locator('#defaultTaxRate').fill(String(tax.rate ?? 0))
  62. await page.locator('#taxLabel').fill(tax.label ?? '')
  63. }
  64. await page
  65. .getByRole('button', { name: tax.inclusive ? 'Inclusive' : 'Exclusive', exact: true })
  66. .click()
  67. }
  68. await page.getByRole('button', { name: 'Save Settings', exact: true }).click()
  69. await expect(page.getByText('Settings saved', { exact: true })).toBeVisible()
  70. }
  71. export interface NumberingSetup {
  72. /** The invoice number format, `{year}` and `{month}` included. */
  73. prefix: string
  74. /** The number the next invoice takes. Empty leaves the sequence alone. */
  75. startNumber?: string
  76. }
  77. /** Settings → Invoice, the numbering half of it, saved. */
  78. export async function setInvoiceNumbering(
  79. page: Page,
  80. { prefix, startNumber = '' }: NumberingSetup
  81. ): Promise<void> {
  82. await page.goto('/settings/invoice')
  83. await fillSettled(page.locator('#invoicePrefix'), prefix)
  84. await fillSettled(page.locator('#invoiceStartNumber'), startNumber)
  85. await page.getByRole('button', { name: 'Save Invoice Settings', exact: true }).click()
  86. await expect(page.getByText('Invoice settings saved', { exact: true })).toBeVisible()
  87. }
  88. export interface QuoteLockSetup {
  89. enabled: boolean
  90. /** When a quote stops being editable: once it goes to the customer, or once they accept. */
  91. trigger: 'sent' | 'accepted'
  92. }
  93. /**
  94. * Settings → Invoice, the quote half of "Locking finished documents", saved.
  95. * Off by default, so a spec that turns it on turns it off again.
  96. */
  97. export async function setQuoteLock(
  98. page: Page,
  99. { enabled, trigger }: QuoteLockSetup
  100. ): Promise<void> {
  101. await page.goto('/settings/invoice')
  102. await settle(page)
  103. // The trigger select is disabled while the switch is off, so the switch goes
  104. // on first and is set to what was asked for after the trigger is chosen.
  105. const toggle = page.locator('#quoteLockEnabled')
  106. await setSwitch(toggle, true)
  107. const wanted = trigger === 'sent' ? 'Once the quote is sent' : 'Once the quote is accepted'
  108. const select = page.locator('#quoteLockTrigger')
  109. await expect(async () => {
  110. await select.click()
  111. await expect(page.getByRole('option', { name: wanted })).toBeVisible({ timeout: 2_000 })
  112. }).toPass({ timeout: 30_000 })
  113. await page.getByRole('option', { name: wanted }).click()
  114. await expect(select).toContainText(wanted)
  115. await setSwitch(toggle, enabled)
  116. await page.getByRole('button', { name: 'Save Invoice Settings', exact: true }).click()
  117. await expect(page.getByText('Invoice settings saved', { exact: true })).toBeVisible()
  118. }
  119. /** What the settings page currently offers as the next invoice number. */
  120. export async function invoiceStartNumber(page: Page): Promise<string> {
  121. await page.goto('/settings/invoice')
  122. const field = page.locator('#invoiceStartNumber')
  123. await expect(field).toBeVisible()
  124. return field.inputValue()
  125. }
  126. /**
  127. * Settings → Localisation: the workshop's clock.
  128. *
  129. * The timezone decides what a booking's wall clock means, and the format
  130. * decides how it is written. Both are set together because a test that reads
  131. * a time off one screen and looks for it on another needs them to agree: the
  132. * calendar prints the workshop's chosen format, while a schedule field prints
  133. * a 24-hour clock whatever the setting says.
  134. *
  135. * Pass an empty timezone to hand it back to the browser's own, which is what
  136. * a workshop that has never chosen sees.
  137. */
  138. export async function setWorkshopClock(
  139. page: Page,
  140. { timezone, format }: { timezone: string; format?: '12h' | '24h' }
  141. ): Promise<void> {
  142. await page.goto('/settings/localization')
  143. await settle(page)
  144. // By label, not by what it currently reads: the date-format select a few
  145. // rows up also shows a value full of slashes.
  146. const picker = page.getByLabel('Timezone')
  147. await expect(async () => {
  148. await picker.click()
  149. await expect(page.getByPlaceholder('Search timezone...')).toBeVisible({ timeout: 2_000 })
  150. }).toPass({ timeout: 30_000 })
  151. if (timezone) {
  152. // The list is long, so it is narrowed to the city, written the way the
  153. // list writes it: "Los Angeles", not "Los_Angeles".
  154. const city = (timezone.split('/').pop() ?? timezone).replace(/_/g, ' ')
  155. await page.getByPlaceholder('Search timezone...').fill(city)
  156. await page
  157. .getByRole('option', { name: timezone.replace(/_/g, ' '), exact: true })
  158. .first()
  159. .click()
  160. } else {
  161. await page.getByRole('option', { name: 'Auto-detect (browser)' }).first().click()
  162. }
  163. if (format) {
  164. const wanted = format === '24h' ? '24-hour (14:30)' : '12-hour (2:30 PM)'
  165. const clock = page.getByLabel('Time Format')
  166. await expect(async () => {
  167. await clock.click()
  168. await expect(page.getByRole('option', { name: wanted })).toBeVisible({ timeout: 2_000 })
  169. }).toPass({ timeout: 30_000 })
  170. await page.getByRole('option', { name: wanted }).click()
  171. }
  172. await page.getByRole('button', { name: 'Save Settings', exact: true }).click()
  173. await expect(page.getByText('Localization settings saved')).toBeVisible({ timeout: 30_000 })
  174. }
  175. /**
  176. * Settings → Payment, the bank account.
  177. *
  178. * The payment panel on a sheet prints nothing at all unless it has a line to
  179. * print, and the seeded workshop has no bank details, no org number and no
  180. * terms. A spec that wants the panel has to give it one, and put it back
  181. * afterwards: every other sheet in the suite is printed from these settings.
  182. */
  183. export async function setBankAccount(page: Page, account: string): Promise<void> {
  184. await page.goto('/settings/payment')
  185. await fillSettled(page.locator('#bankAccount'), account)
  186. await page.getByRole('button', { name: 'Save Payment Settings', exact: true }).click()
  187. await expect(page.getByText('Payment settings saved', { exact: true })).toBeVisible()
  188. }
  189. /** What the workshop currently has as its bank account, which may be nothing. */
  190. export async function bankAccount(page: Page): Promise<string> {
  191. await page.goto('/settings/payment')
  192. const field = page.locator('#bankAccount')
  193. await expect(field).toBeVisible()
  194. return field.inputValue()
  195. }