utilities.spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /// <reference types="cypress" />
  2. context('Utilities', () => {
  3. beforeEach(() => {
  4. cy.visit('https://example.cypress.io/utilities')
  5. })
  6. it('Cypress._ - call a lodash method', () => {
  7. // https://on.cypress.io/_
  8. cy.request('https://jsonplaceholder.cypress.io/users')
  9. .then((response) => {
  10. let ids = Cypress._.chain(response.body).map('id').take(3).value()
  11. expect(ids).to.deep.eq([1, 2, 3])
  12. })
  13. })
  14. it('Cypress.$ - call a jQuery method', () => {
  15. // https://on.cypress.io/$
  16. let $li = Cypress.$('.utility-jquery li:first')
  17. cy.wrap($li)
  18. .should('not.have.class', 'active')
  19. .click()
  20. .should('have.class', 'active')
  21. })
  22. it('Cypress.Blob - blob utilities and base64 string conversion', () => {
  23. // https://on.cypress.io/blob
  24. cy.get('.utility-blob').then(($div) => {
  25. // https://github.com/nolanlawson/blob-util#imgSrcToDataURL
  26. // get the dataUrl string for the javascript-logo
  27. return Cypress.Blob.imgSrcToDataURL('https://example.cypress.io/assets/img/javascript-logo.png', undefined, 'anonymous')
  28. .then((dataUrl) => {
  29. // create an <img> element and set its src to the dataUrl
  30. let img = Cypress.$('<img />', { src: dataUrl })
  31. // need to explicitly return cy here since we are initially returning
  32. // the Cypress.Blob.imgSrcToDataURL promise to our test
  33. // append the image
  34. $div.append(img)
  35. cy.get('.utility-blob img').click()
  36. .should('have.attr', 'src', dataUrl)
  37. })
  38. })
  39. })
  40. it('Cypress.minimatch - test out glob patterns against strings', () => {
  41. // https://on.cypress.io/minimatch
  42. let matching = Cypress.minimatch('/users/1/comments', '/users/*/comments', {
  43. matchBase: true,
  44. })
  45. expect(matching, 'matching wildcard').to.be.true
  46. matching = Cypress.minimatch('/users/1/comments/2', '/users/*/comments', {
  47. matchBase: true,
  48. })
  49. expect(matching, 'comments').to.be.false
  50. // ** matches against all downstream path segments
  51. matching = Cypress.minimatch('/foo/bar/baz/123/quux?a=b&c=2', '/foo/**', {
  52. matchBase: true,
  53. })
  54. expect(matching, 'comments').to.be.true
  55. // whereas * matches only the next path segment
  56. matching = Cypress.minimatch('/foo/bar/baz/123/quux?a=b&c=2', '/foo/*', {
  57. matchBase: false,
  58. })
  59. expect(matching, 'comments').to.be.false
  60. })
  61. it('Cypress.Promise - instantiate a bluebird promise', () => {
  62. // https://on.cypress.io/promise
  63. let waited = false
  64. /**
  65. * @return Bluebird<string>
  66. */
  67. function waitOneSecond () {
  68. // return a promise that resolves after 1 second
  69. // @ts-ignore TS2351 (new Cypress.Promise)
  70. return new Cypress.Promise((resolve, reject) => {
  71. setTimeout(() => {
  72. // set waited to true
  73. waited = true
  74. // resolve with 'foo' string
  75. resolve('foo')
  76. }, 1000)
  77. })
  78. }
  79. cy.then(() => {
  80. // return a promise to cy.then() that
  81. // is awaited until it resolves
  82. // @ts-ignore TS7006
  83. return waitOneSecond().then((str) => {
  84. expect(str).to.eq('foo')
  85. expect(waited).to.be.true
  86. })
  87. })
  88. })
  89. })