Advertisement
Guest User

Untitled

a guest
May 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. test('two plus two is four', () => {
  2. expect(2 + 2).toBe(4)
  3. })
  4.  
  5. test('object assignment', () => {
  6. const data = { one: 1 }
  7. data['two'] = 2
  8. expect(data).toEqual({ one: 1, two: 2 })
  9. })
  10.  
  11. test('adding positive numbers is not zero', () => {
  12. for (let a = 1; a < 10; a++) {
  13. for (let b = 1; b < 10; b++) {
  14. expect(a + b).not.toBe(0)
  15. }
  16. }
  17. })
  18.  
  19. test('null', () => {
  20. const n = null
  21. expect(n).toBeNull()
  22. expect(n).toBeDefined()
  23. expect(n).not.toBeUndefined()
  24. expect(n).not.toBeTruthy()
  25. expect(n).toBeFalsy()
  26. })
  27.  
  28. test('zero', () => {
  29. const z = 0
  30. expect(z).not.toBeNull()
  31. expect(z).toBeDefined()
  32. expect(z).not.toBeUndefined()
  33. expect(z).not.toBeTruthy()
  34. expect(z).toBeFalsy()
  35. expect(z).toBe(0)
  36. })
  37.  
  38. test('two plus two', () => {
  39. const value = 2 + 2
  40. expect(value).toBeGreaterThan(3)
  41. expect(value).toBeGreaterThanOrEqual(3)
  42. expect(value).toBeLessThan(5)
  43. expect(value).toBeLessThanOrEqual(4.5)
  44. expect(value).toBe(4)
  45. expect(value).toEqual(4)
  46. })
  47.  
  48. test('adding floating point number', () => {
  49. const value = 0.1 + 0.2
  50. expect(value).toBeCloseTo(0.3)
  51. })
  52.  
  53. test('there is no I in team', () => {
  54. expect('team').not.toMatch(/I/)
  55. })
  56.  
  57. test('but there is a "stop" in Christoph', () => {
  58. expect('Christoph').toMatch(/stop/)
  59. })
  60.  
  61. test('the shopping list has beer on it', () => {
  62. const shoppingList = [
  63. 'diapers',
  64. 'kleenex',
  65. 'trash bags',
  66. 'paper towels',
  67. 'beer',
  68. ];
  69. expect(shoppingList).toContain('beer')
  70. expect(new Set(shoppingList)).toContain('beer')
  71. })
  72.  
  73. class ConfigError extends Error {}
  74. function compileAndroidCode() {
  75. throw new ConfigError('you are using the wrong JDK')
  76. }
  77.  
  78. test('compling android goes as expected', () => {
  79. expect(compileAndroidCode).toThrow()
  80. expect(compileAndroidCode).toThrow(ConfigError)
  81.  
  82. expect(compileAndroidCode).toThrow('you are using the wrong JDK')
  83. expect(compileAndroidCode).toThrow(/JDK/)
  84. })
  85.  
  86. // ===================== Callbacks ============================
  87. test('the data is peanut butter', done => {
  88. function fetchData(callback) {
  89. callback('peanut butter')
  90. }
  91.  
  92. function callback(data) {
  93. setTimeout(() => {
  94. expect(data).toBe('peanut butter')
  95. done()
  96. }, 1000)
  97. }
  98.  
  99. fetchData(callback)
  100. })
  101.  
  102. // ==================== Promise ================================
  103. function promiseFetchData(success) {
  104. return new Promise((resolve, reject) => {
  105. setTimeout(() => {
  106. if (!success) return reject('error')
  107. return resolve('peanut butter')
  108. }, 100)
  109. })
  110. }
  111.  
  112. it('the data is peanut butter', () => {
  113. const resp = 'qqq'
  114. const mockFunction = jest.fn(promiseFetchData)
  115. mockFunction.mockResolvedValue(resp)
  116.  
  117. return mockFunction().then(data => {
  118. expect(data).toBe(resp)
  119. })
  120. })
  121.  
  122. test('the fetch fails with an error', () => {
  123. return promiseFetchData(false).catch(error => {
  124. expect.assertions(1)
  125. expect(error).toMatch('error')
  126. })
  127. })
  128.  
  129. test('promise: the data is peanut butter', () => {
  130. return expect(promiseFetchData(true)).resolves.toBe('peanut butter')
  131. })
  132.  
  133. test('promise: the data is peanut butter', () => {
  134. return expect(promiseFetchData(false)).rejects.toBe('error')
  135. })
  136.  
  137. // ==================== Async/Await ================================
  138. async function asyncFetchData(success) {
  139. return new Promise((resolve, reject) => {
  140. setTimeout(() => {
  141. if (!success) return reject('error')
  142. return resolve('peanut butter')
  143. }, 100)
  144. })
  145. }
  146.  
  147. test('the data is peanut butter', async () => {
  148. expect.assertions(1)
  149. const data = await asyncFetchData(true)
  150. expect(data).toBe('peanut butter')
  151. })
  152.  
  153. test('the fetch data fails with an error', async () => {
  154. expect.assertions(1)
  155. try {
  156. await asyncFetchData(false)
  157. } catch(e) {
  158. expect(e).toMatch('error')
  159. }
  160. })
  161.  
  162. test('the data is peanut butter', async () => {
  163. await expect(asyncFetchData(true)).resolves.toBe('peanut butter')
  164. })
  165.  
  166. test('the fetch fails with an error', async () => {
  167. await expect(asyncFetchData(false)).rejects.toMatch('error')
  168. })
  169.  
  170. // ======================= Mock Functions ===============================
  171. function forEach(items, callback) {
  172. for (let index = 0; index < items.length; index++) {
  173. callback(items[index], 99)
  174. }
  175. }
  176.  
  177. const mockCallback = jest.fn(x => 42 + x)
  178. test('forEach', () => {
  179. forEach([0, 1], mockCallback)
  180. expect(mockCallback.mock.calls.length).toBe(2)
  181. expect(mockCallback.mock.calls[0][0]).toBe(0)
  182. expect(mockCallback.mock.calls[1][0]).toBe(1)
  183. expect(mockCallback.mock.results[0].value).toBe(42)
  184. expect(mockCallback.mock.results[1].value).toBe(43)
  185. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement