|
- import {FastifyInstance} from 'fastify'
- import {PrismaClient as RealPrismaClient} from '@prisma/client'
- import {container} from 'tsyringe'
- import {models} from '@tonality/library-common'
- import Uuid from '@tonality/library-uuid'
- import {build} from '../../helper'
- import {MockPrismaRepository} from '../../../src/utils/mocks'
-
- jest.mock('@prisma/client')
- const PrismaClient = RealPrismaClient as jest.Mock
-
- describe('ringtone: /api/ringtones', () => {
- let app: FastifyInstance
-
- beforeEach(() => {
- PrismaClient.mockImplementationOnce(() => ({
- user: new MockPrismaRepository<models.User>([
- {
- id: Uuid.parse('00000000-0000-0000-0000-000000000000'),
- username: 'TheoryOfNekomata',
- password: 'dummy password',
- },
- ], 'id'),
- ringtone: new MockPrismaRepository<models.Ringtone>([
- {
- id: Uuid.parse('00000000-0000-0000-0000-000000000000'),
- name: 'Ringtone 1',
- data: '4c4',
- tempo: 120,
- createdAt: new Date('2021-05-01'),
- updatedAt: new Date('2021-05-01'),
- deletedAt: null,
- composerUserId: Uuid.parse('00000000-0000-0000-0000-000000000000'),
- },
- {
- id: Uuid.parse('8df2751c-4c05-4831-9408-92af3b4a63e8'),
- name: 'Unique Ringtone 2',
- data: '4c4 8c4',
- tempo: 120,
- createdAt: new Date('2021-05-01'),
- updatedAt: new Date('2021-05-01'),
- deletedAt: null,
- composerUserId: Uuid.parse('00000000-0000-0000-0000-000000000000'),
- },
- {
- id: Uuid.parse('c8423931-6a08-41f6-8320-a5ce5367179f'),
- name: 'Deleted Ringtone 1',
- data: '4c4',
- tempo: 120,
- createdAt: new Date('2021-05-01'),
- updatedAt: new Date('2021-05-01'),
- deletedAt: new Date('2021-05-05'),
- composerUserId: Uuid.parse('00000000-0000-0000-0000-000000000000'),
- },
- ], 'id')
- }))
- })
-
- beforeEach(async () => {
- app = await build()
- })
-
- afterEach(async () => {
- container.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',
- tempo: 120,
- composerUserId: '00000000-0000-0000-0000-000000000000',
- })
- })
- const parsedPayload = JSON.parse(res.payload)
- expect(parsedPayload.data).toEqual(expect.objectContaining({
- name: 'New Ringtone',
- data: '4c4',
- tempo: 120,
- }))
- })
- })
-
- describe('on updating', () => {
- it('should store the updated data', async () => {
- const res = await app.inject({
- url: '/api/ringtones/00000000-0000-0000-0000-000000000000',
- method: 'PATCH',
- headers: {
- 'Content-Type': 'application/json',
- },
- payload: JSON.stringify({
- name: 'Updated Ringtone',
- data: '4c8',
- tempo: 120,
- })
- })
- const parsedPayload = JSON.parse(res.payload)
- expect(parsedPayload.data).toEqual(expect.objectContaining({
- name: 'Updated Ringtone',
- data: '4c8',
- tempo: 120,
- }))
- 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-0000-000000000000/delete',
- method: 'POST',
- })
- const parsedPayload = JSON.parse(res.payload)
- expect(parsedPayload.data).toEqual(expect.objectContaining({
- 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-0000-000000000000/delete',
- method: 'DELETE',
- })
- const parsedPayload = JSON.parse(res.payload)
- expect(parsedPayload.data).toEqual(expect.objectContaining({
- deletedAt: null,
- }))
- })
- })
-
- describe('on hard deletion', () => {
- it('should be removed', async () => {
- const res = await app.inject({
- url: '/api/ringtones/00000000-0000-0000-0000-000000000000',
- method: 'DELETE',
- })
- expect(res.statusCode).toBe(204)
- })
- })
- })
|