|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import {FastifyInstance} from 'fastify'
- import {build} from '../../helper'
- import ringtoneModule from '../../../src/modules/ringtone'
- import MockRingtoneRepository from '../../mocks/repositories/Ringtone'
-
- describe('ringtone', () => {
- let app: FastifyInstance
-
- beforeEach(async () => {
- ringtoneModule.registerInstance('RingtoneRepository', new MockRingtoneRepository([
- {
- id: '00000000-0000-0000-000000000000',
- name: 'Ringtone 1',
- data: '4c4',
- createdAt: new Date('2021-05-01'),
- updatedAt: new Date('2021-05-01'),
- deletedAt: null,
- composerId: '00000000-0000-0000-000000000000',
- },
- {
- id: '00000000-0000-0000-000000000001',
- name: 'Unique Ringtone 2',
- data: '4c4 8c4',
- createdAt: new Date('2021-05-01'),
- updatedAt: new Date('2021-05-01'),
- deletedAt: null,
- composerId: '00000000-0000-0000-000000000000',
- },
- {
- id: '00000000-0000-0000-000000000002',
- name: 'Deleted Ringtone 1',
- data: '4c4',
- createdAt: new Date('2021-05-01'),
- updatedAt: new Date('2021-05-01'),
- deletedAt: new Date('2021-05-05'),
- composerId: '00000000-0000-0000-000000000000',
- }
- ]))
- app = await build()
- })
-
- afterEach(async () => {
- ringtoneModule.clearInstances()
- await app.close()
- })
-
- describe('on searching', () => {
- it('should send the data to the front-end', async () => {
- const res = await app.inject({
- url: '/api/search/ringtones',
- query: {
- q: 'Unique',
- },
- method: 'GET',
- })
- const parsedPayload = JSON.parse(res.payload)
- expect(Array.isArray(parsedPayload.data)).toBe(true)
- })
- })
-
- describe('on browsing', () => {
- it('should send the data to the front-end', async () => {
- const res = await app.inject({
- url: '/api/ringtones',
- method: 'GET',
- })
- const parsedPayload = JSON.parse(res.payload)
- expect(Array.isArray(parsedPayload.data)).toBe(true)
- })
- })
-
- describe('on creation', () => {
- it('should store the data', async () => {
- const res = await app.inject({
- url: '/api/ringtones',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- payload: JSON.stringify({
- name: 'New Ringtone',
- data: '4c4',
- composerId: '00000000-0000-0000-000000000000',
- })
- })
- const parsedPayload = JSON.parse(res.payload)
- expect(parsedPayload.data).toEqual(expect.objectContaining({
- name: 'New Ringtone',
- data: '4c4',
- composerId: '00000000-0000-0000-000000000000',
- }))
- })
- })
-
- describe('on updating', () => {
- it('should store the updated data', async () => {
- const res = await app.inject({
- url: '/api/ringtones/00000000-0000-0000-000000000000',
- method: 'PATCH',
- headers: {
- 'Content-Type': 'application/json',
- },
- payload: JSON.stringify({
- name: 'Updated Ringtone',
- data: '4c8',
- composerId: '00000000-0000-0000-000000000000',
- })
- })
- const parsedPayload = JSON.parse(res.payload)
- expect(parsedPayload.data).toEqual(expect.objectContaining({
- name: 'Updated Ringtone',
- data: '4c8',
- composerId: '00000000-0000-0000-000000000000',
- }))
- expect(parsedPayload.data.createdAt).not.toEqual(parsedPayload.data.updatedAt)
- })
- })
-
- describe('on soft deletion', () => {
- it('should be tagged as deleted', async () => {
- const res = await app.inject({
- url: '/api/ringtones/00000000-0000-0000-000000000000/delete',
- method: 'POST',
- })
- const parsedPayload = JSON.parse(res.payload)
- expect(parsedPayload.data).toEqual(expect.objectContaining({
- id: '00000000-0000-0000-000000000000',
- deletedAt: expect.any(String),
- }))
- })
- })
-
- describe('on undoing deletion', () => {
- it('should be untagged as deleted', async () => {
- const res = await app.inject({
- url: '/api/ringtones/00000000-0000-0000-000000000000/delete',
- method: 'DELETE',
- })
- const parsedPayload = JSON.parse(res.payload)
- expect(parsedPayload.data).toEqual(expect.objectContaining({
- id: '00000000-0000-0000-000000000000',
- deletedAt: null,
- }))
- })
- })
-
- describe('on hard deletion', () => {
- it('should be removed', async () => {
- const res = await app.inject({
- url: '/api/ringtones/00000000-0000-0000-000000000000',
- method: 'DELETE',
- })
- expect(res.statusCode).toBe(204)
- })
- })
- })
|