assertions.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*!
  2. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. import { ZipReader } from '@zip.js/zip.js'
  6. /**
  7. * Assert that a file contains a list of expected files
  8. * @param expectedFiles List of expected filenames
  9. * @example
  10. * ```js
  11. * cy.readFile('file', null, { ... })
  12. * .should(zipFileContains(['file.txt']))
  13. * ```
  14. */
  15. export function zipFileContains(expectedFiles: string[]) {
  16. return async (buffer: Buffer) => {
  17. const blob = new Blob([buffer])
  18. const zip = new ZipReader(blob.stream())
  19. // check the real file names
  20. const entries = (await zip.getEntries()).map((e) => e.filename).sort()
  21. console.info('Zip contains entries:', entries)
  22. expect(entries).to.deep.equal(expectedFiles.sort())
  23. }
  24. }
  25. /**
  26. * Check validity of an input element
  27. * @param validity The expected validity message (empty string means it is valid)
  28. * @example
  29. * ```js
  30. * cy.findByRole('textbox')
  31. * .should(haveValidity(/must not be empty/i))
  32. * ```
  33. */
  34. export const haveValidity = (validity: string | RegExp) => {
  35. if (typeof validity === 'string') {
  36. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.equal(validity)
  37. }
  38. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.match(validity)
  39. }