settings.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. export interface InvoiceLockSetup {
  120. enabled: boolean
  121. trigger: 'sent' | 'paid'
  122. }
  123. /**
  124. * Settings → Invoice, the invoice half of "Locking finished documents", saved.
  125. * Off by default, so a spec that turns it on turns it off again. Invoices sent
  126. * before it was turned on stay editable, so a spec sends its job afterwards.
  127. */
  128. export async function setInvoiceLock(
  129. page: Page,
  130. { enabled, trigger }: InvoiceLockSetup
  131. ): Promise<void> {
  132. await page.goto('/settings/invoice')
  133. await settle(page)
  134. // As with the quote lock: the trigger is disabled while the switch is off.
  135. const toggle = page.locator('#invoiceLockEnabled')
  136. await setSwitch(toggle, true)
  137. const wanted =
  138. trigger === 'sent' ? 'Once the invoice is sent' : 'Once the invoice is paid in full'
  139. const select = page.locator('#invoiceLockTrigger')
  140. await expect(async () => {
  141. await select.click()
  142. await expect(page.getByRole('option', { name: wanted })).toBeVisible({ timeout: 2_000 })
  143. }).toPass({ timeout: 30_000 })
  144. await page.getByRole('option', { name: wanted }).click()
  145. await expect(select).toContainText(wanted)
  146. await setSwitch(toggle, enabled)
  147. await page.getByRole('button', { name: 'Save Invoice Settings', exact: true }).click()
  148. await expect(page.getByText('Invoice settings saved', { exact: true })).toBeVisible()
  149. }
  150. /** What the settings page currently offers as the next invoice number. */
  151. export async function invoiceStartNumber(page: Page): Promise<string> {
  152. await page.goto('/settings/invoice')
  153. const field = page.locator('#invoiceStartNumber')
  154. await expect(field).toBeVisible()
  155. return field.inputValue()
  156. }
  157. /**
  158. * Settings → Localisation: the workshop's clock.
  159. *
  160. * The timezone decides what a booking's wall clock means, and the format
  161. * decides how it is written. Both are set together because a test that reads
  162. * a time off one screen and looks for it on another needs them to agree: the
  163. * calendar prints the workshop's chosen format, while a schedule field prints
  164. * a 24-hour clock whatever the setting says.
  165. *
  166. * Pass an empty timezone to hand it back to the browser's own, which is what
  167. * a workshop that has never chosen sees.
  168. */
  169. export async function setWorkshopClock(
  170. page: Page,
  171. { timezone, format }: { timezone: string; format?: '12h' | '24h' }
  172. ): Promise<void> {
  173. await page.goto('/settings/localization')
  174. await settle(page)
  175. // By label, not by what it currently reads: the date-format select a few
  176. // rows up also shows a value full of slashes.
  177. const picker = page.getByLabel('Timezone')
  178. await expect(async () => {
  179. await picker.click()
  180. await expect(page.getByPlaceholder('Search timezone...')).toBeVisible({ timeout: 2_000 })
  181. }).toPass({ timeout: 30_000 })
  182. if (timezone) {
  183. // The list is long, so it is narrowed to the city, written the way the
  184. // list writes it: "Los Angeles", not "Los_Angeles".
  185. const city = (timezone.split('/').pop() ?? timezone).replace(/_/g, ' ')
  186. await page.getByPlaceholder('Search timezone...').fill(city)
  187. await page
  188. .getByRole('option', { name: timezone.replace(/_/g, ' '), exact: true })
  189. .first()
  190. .click()
  191. } else {
  192. await page.getByRole('option', { name: 'Auto-detect (browser)' }).first().click()
  193. }
  194. if (format) {
  195. const wanted = format === '24h' ? '24-hour (14:30)' : '12-hour (2:30 PM)'
  196. const clock = page.getByLabel('Time Format')
  197. await expect(async () => {
  198. await clock.click()
  199. await expect(page.getByRole('option', { name: wanted })).toBeVisible({ timeout: 2_000 })
  200. }).toPass({ timeout: 30_000 })
  201. await page.getByRole('option', { name: wanted }).click()
  202. }
  203. await page.getByRole('button', { name: 'Save Settings', exact: true }).click()
  204. await expect(page.getByText('Localization settings saved')).toBeVisible({ timeout: 30_000 })
  205. }
  206. /**
  207. * Settings → Payment, the bank account.
  208. *
  209. * The payment panel on a sheet prints nothing at all unless it has a line to
  210. * print, and the seeded workshop has no bank details, no org number and no
  211. * terms. A spec that wants the panel has to give it one, and put it back
  212. * afterwards: every other sheet in the suite is printed from these settings.
  213. */
  214. export async function setBankAccount(page: Page, account: string): Promise<void> {
  215. await page.goto('/settings/payment')
  216. await fillSettled(page.locator('#bankAccount'), account)
  217. await page.getByRole('button', { name: 'Save Payment Settings', exact: true }).click()
  218. await expect(page.getByText('Payment settings saved', { exact: true })).toBeVisible()
  219. }
  220. /** What the workshop currently has as its bank account, which may be nothing. */
  221. export async function bankAccount(page: Page): Promise<string> {
  222. await page.goto('/settings/payment')
  223. const field = page.locator('#bankAccount')
  224. await expect(field).toBeVisible()
  225. return field.inputValue()
  226. }
  227. export interface WarrantySetup {
  228. /** What a new quote or work order says before anybody touches it. */
  229. newDocumentsSay: 'Nothing' | 'Warranty included' | 'No warranty'
  230. months?: number
  231. distance?: number
  232. terms?: string
  233. notIncludedText?: string
  234. /** Whether the defaults reach new quotes, and new work orders. On unless said otherwise. */
  235. onQuotes?: boolean
  236. onWorkOrders?: boolean
  237. }
  238. /**
  239. * Settings → Warranty, saved. Nothing is set on a seeded workshop, so a spec
  240. * that writes these removes them afterwards (`forgetWorkshopSetting`): every
  241. * other quote and invoice in the suite is printed without a warranty panel.
  242. */
  243. export async function setWarrantyDefaults(page: Page, warranty: WarrantySetup): Promise<void> {
  244. await page.goto('/settings/warranty')
  245. await settle(page)
  246. const choice = page.getByRole('radio', { name: warranty.newDocumentsSay, exact: true })
  247. await expect(async () => {
  248. await choice.click()
  249. await expect(choice).toHaveAttribute('aria-checked', 'true', { timeout: 2_000 })
  250. }).toPass({ timeout: 30_000 })
  251. const text = (value: number | string | undefined) => (value === undefined ? '' : String(value))
  252. await fillSettled(page.locator('#warrantyDefaultMonths'), text(warranty.months))
  253. await fillSettled(page.locator('#warrantyDefaultMileage'), text(warranty.distance))
  254. await fillSettled(page.locator('#warrantyDefaultTerms'), text(warranty.terms))
  255. await fillSettled(page.locator('#warrantyNotIncludedText'), text(warranty.notIncludedText))
  256. await setSwitch(page.locator('#warrantyApplyToQuotes'), warranty.onQuotes ?? true)
  257. await setSwitch(page.locator('#warrantyApplyToWorkOrders'), warranty.onWorkOrders ?? true)
  258. await page.getByRole('button', { name: 'Save warranty settings', exact: true }).click()
  259. await expect(page.getByText('Warranty settings saved', { exact: true })).toBeVisible()
  260. }