Ringtone app
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

157 lignes
4.2 KiB

  1. import {FastifyInstance} from 'fastify'
  2. import {build} from '../../helper'
  3. import ringtoneModule from '../../../src/modules/ringtone'
  4. import MockRingtoneRepository from '../../mocks/repositories/Ringtone'
  5. describe('ringtone', () => {
  6. let app: FastifyInstance
  7. beforeEach(async () => {
  8. ringtoneModule.registerInstance('RingtoneRepository', new MockRingtoneRepository([
  9. {
  10. id: '00000000-0000-0000-000000000000',
  11. name: 'Ringtone 1',
  12. data: '4c4',
  13. createdAt: new Date('2021-05-01'),
  14. updatedAt: new Date('2021-05-01'),
  15. deletedAt: null,
  16. composerId: '00000000-0000-0000-000000000000',
  17. },
  18. {
  19. id: '00000000-0000-0000-000000000001',
  20. name: 'Unique Ringtone 2',
  21. data: '4c4 8c4',
  22. createdAt: new Date('2021-05-01'),
  23. updatedAt: new Date('2021-05-01'),
  24. deletedAt: null,
  25. composerId: '00000000-0000-0000-000000000000',
  26. },
  27. {
  28. id: '00000000-0000-0000-000000000002',
  29. name: 'Deleted Ringtone 1',
  30. data: '4c4',
  31. createdAt: new Date('2021-05-01'),
  32. updatedAt: new Date('2021-05-01'),
  33. deletedAt: new Date('2021-05-05'),
  34. composerId: '00000000-0000-0000-000000000000',
  35. }
  36. ]))
  37. app = await build()
  38. })
  39. afterEach(async () => {
  40. ringtoneModule.clearInstances()
  41. await app.close()
  42. })
  43. describe('on searching', () => {
  44. it('should send the data to the front-end', async () => {
  45. const res = await app.inject({
  46. url: '/api/search/ringtones',
  47. query: {
  48. q: 'Unique',
  49. },
  50. method: 'GET',
  51. })
  52. const parsedPayload = JSON.parse(res.payload)
  53. expect(Array.isArray(parsedPayload.data)).toBe(true)
  54. })
  55. })
  56. describe('on browsing', () => {
  57. it('should send the data to the front-end', async () => {
  58. const res = await app.inject({
  59. url: '/api/ringtones',
  60. method: 'GET',
  61. })
  62. const parsedPayload = JSON.parse(res.payload)
  63. expect(Array.isArray(parsedPayload.data)).toBe(true)
  64. })
  65. })
  66. describe('on creation', () => {
  67. it('should store the data', async () => {
  68. const res = await app.inject({
  69. url: '/api/ringtones',
  70. method: 'POST',
  71. headers: {
  72. 'Content-Type': 'application/json',
  73. },
  74. payload: JSON.stringify({
  75. name: 'New Ringtone',
  76. data: '4c4',
  77. composerId: '00000000-0000-0000-000000000000',
  78. })
  79. })
  80. const parsedPayload = JSON.parse(res.payload)
  81. expect(parsedPayload.data).toEqual(expect.objectContaining({
  82. name: 'New Ringtone',
  83. data: '4c4',
  84. composerId: '00000000-0000-0000-000000000000',
  85. }))
  86. })
  87. })
  88. describe('on updating', () => {
  89. it('should store the updated data', async () => {
  90. const res = await app.inject({
  91. url: '/api/ringtones/00000000-0000-0000-000000000000',
  92. method: 'PATCH',
  93. headers: {
  94. 'Content-Type': 'application/json',
  95. },
  96. payload: JSON.stringify({
  97. name: 'Updated Ringtone',
  98. data: '4c8',
  99. composerId: '00000000-0000-0000-000000000000',
  100. })
  101. })
  102. const parsedPayload = JSON.parse(res.payload)
  103. expect(parsedPayload.data).toEqual(expect.objectContaining({
  104. name: 'Updated Ringtone',
  105. data: '4c8',
  106. composerId: '00000000-0000-0000-000000000000',
  107. }))
  108. expect(parsedPayload.data.createdAt).not.toEqual(parsedPayload.data.updatedAt)
  109. })
  110. })
  111. describe('on soft deletion', () => {
  112. it('should be tagged as deleted', async () => {
  113. const res = await app.inject({
  114. url: '/api/ringtones/00000000-0000-0000-000000000000/delete',
  115. method: 'POST',
  116. })
  117. const parsedPayload = JSON.parse(res.payload)
  118. expect(parsedPayload.data).toEqual(expect.objectContaining({
  119. id: '00000000-0000-0000-000000000000',
  120. deletedAt: expect.any(String),
  121. }))
  122. })
  123. })
  124. describe('on undoing deletion', () => {
  125. it('should be untagged as deleted', async () => {
  126. const res = await app.inject({
  127. url: '/api/ringtones/00000000-0000-0000-000000000000/delete',
  128. method: 'DELETE',
  129. })
  130. const parsedPayload = JSON.parse(res.payload)
  131. expect(parsedPayload.data).toEqual(expect.objectContaining({
  132. id: '00000000-0000-0000-000000000000',
  133. deletedAt: null,
  134. }))
  135. })
  136. })
  137. describe('on hard deletion', () => {
  138. it('should be removed', async () => {
  139. const res = await app.inject({
  140. url: '/api/ringtones/00000000-0000-0000-000000000000',
  141. method: 'DELETE',
  142. })
  143. expect(res.statusCode).toBe(204)
  144. })
  145. })
  146. })