|
| 1 | +import { IocContract } from '@adonisjs/fold' |
| 2 | + |
| 3 | +import { Merchant } from './model' |
| 4 | +import { MerchantService } from './service' |
| 5 | + |
| 6 | +import { createTestApp, TestContainer } from '../tests/app' |
| 7 | +import { truncateTables } from '../tests/tableManager' |
| 8 | +import { Config } from '../config/app' |
| 9 | + |
| 10 | +import { initIocContainer } from '../' |
| 11 | +import { AppServices } from '../app' |
| 12 | + |
| 13 | +describe('Merchant Service', (): void => { |
| 14 | + let deps: IocContract<AppServices> |
| 15 | + let appContainer: TestContainer |
| 16 | + let merchantService: MerchantService |
| 17 | + |
| 18 | + beforeAll(async (): Promise<void> => { |
| 19 | + deps = initIocContainer({ |
| 20 | + ...Config |
| 21 | + }) |
| 22 | + |
| 23 | + appContainer = await createTestApp(deps) |
| 24 | + merchantService = await deps.use('merchantService') |
| 25 | + }) |
| 26 | + |
| 27 | + afterEach(async (): Promise<void> => { |
| 28 | + jest.useRealTimers() |
| 29 | + await truncateTables(deps) |
| 30 | + }) |
| 31 | + |
| 32 | + afterAll(async (): Promise<void> => { |
| 33 | + await appContainer.shutdown() |
| 34 | + }) |
| 35 | + |
| 36 | + describe('create', (): void => { |
| 37 | + test('creates a merchant', async (): Promise<void> => { |
| 38 | + const merchant = await merchantService.create('Test merchant') |
| 39 | + expect(merchant).toEqual({ id: merchant.id, name: 'Test merchant' }) |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + describe('delete', (): void => { |
| 44 | + test('soft deletes an existing merchant', async (): Promise<void> => { |
| 45 | + const created = await merchantService.create('Test merchant') |
| 46 | + |
| 47 | + const result = await merchantService.delete(created.id) |
| 48 | + expect(result).toBe(true) |
| 49 | + |
| 50 | + const deletedMerchant = await Merchant.query() |
| 51 | + .findById(created.id) |
| 52 | + .whereNotNull('deletedAt') |
| 53 | + expect(deletedMerchant).toBeDefined() |
| 54 | + expect(deletedMerchant?.deletedAt).toBeDefined() |
| 55 | + }) |
| 56 | + |
| 57 | + test('returns false for already deleted merchant', async (): Promise<void> => { |
| 58 | + const created = await merchantService.create('Test merchant') |
| 59 | + |
| 60 | + await merchantService.delete(created.id) |
| 61 | + const secondDelete = await merchantService.delete(created.id) |
| 62 | + expect(secondDelete).toBe(false) |
| 63 | + }) |
| 64 | + }) |
| 65 | +}) |
0 commit comments