Skip to content

Commit

Permalink
Merge branch 'feature/core-tests' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobuddy committed Jun 4, 2024
2 parents 346ae23 + 056d57d commit 3077fab
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 31 deletions.
3 changes: 1 addition & 2 deletions packages/core/manifest/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { AuthModule } from './auth/auth.module'
import databaseConfig from './config/database'
import generalConfig from './config/general'
import pathsConfig from './config/paths'
import yamlConfig from './config/yaml'
import { CrudModule } from './crud/crud.module'
import { EntityModule } from './entity/entity.module'
import { EntityLoaderService } from './entity/services/entity-loader/entity-loader.service'
Expand All @@ -22,7 +21,7 @@ import { HealthModule } from './health/health.module'
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [generalConfig, databaseConfig, yamlConfig, pathsConfig]
load: [generalConfig, databaseConfig, pathsConfig]
}),

TypeOrmModule.forRootAsync({
Expand Down
7 changes: 0 additions & 7 deletions packages/core/manifest/src/config/yaml.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Test, TestingModule } from '@nestjs/testing'
import { EntityLoaderService } from './entity-loader.service'
import { ManifestService } from '../../../manifest/services/manifest/manifest.service'

describe('EntityLoaderService', () => {
let service: EntityLoaderService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [EntityLoaderService]
providers: [
EntityLoaderService,
{
provide: ManifestService,
useValue: {
getEntityManifest: jest.fn()
}
}
]
}).compile()

service = module.get<EntityLoaderService>(EntityLoaderService)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { Test, TestingModule } from '@nestjs/testing'
import { EntityService } from './entity.service'
import { ManifestService } from '../../../manifest/services/manifest/manifest.service'
import { DataSource } from 'typeorm'

describe('EntityService', () => {
let service: EntityService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [EntityService]
providers: [
EntityService,
{
provide: ManifestService,
useValue: {
getEntityManifest: jest.fn()
}
},
{
provide: DataSource,
useValue: {
entityMetadatas: [],
getRepository: jest.fn()
}
}
]
}).compile()

service = module.get<EntityService>(EntityService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@ describe('PropertyService', () => {
it('should be defined', () => {
expect(service).toBeDefined()
})

it('should return the seed value for a property', () => {
const propertyManifest = {
type: 'string',
options: {
minLength: 5,
maxLength: 10
}
} as any

expect(service.getSeedValue(propertyManifest)).not.toBeNull()
})
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { Test, TestingModule } from '@nestjs/testing'
import { RelationshipService } from './relationship.service'
import { ManifestService } from '../../../manifest/services/manifest/manifest.service'

describe('RelationshipService', () => {
let service: RelationshipService

const mockSeedCount = 50
const dummyRelationManifest = {
name: 'owner',
entity: 'User'
}

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [RelationshipService]
providers: [
RelationshipService,
{
provide: ManifestService,
useValue: {
getEntityManifest: jest.fn().mockReturnValue({
seedCount: mockSeedCount
})
}
}
]
}).compile()

service = module.get<RelationshipService>(RelationshipService)
Expand All @@ -15,4 +32,11 @@ describe('RelationshipService', () => {
it('should be defined', () => {
expect(service).toBeDefined()
})

it('should return a seed value between 1 and the seed count', () => {
const seedValue = service.getSeedValue(dummyRelationManifest)

expect(seedValue).toBeGreaterThanOrEqual(1)
expect(seedValue).toBeLessThanOrEqual(mockSeedCount)
})
})
24 changes: 14 additions & 10 deletions packages/core/manifest/src/health/health.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HealthController } from './health.controller';
import { Test, TestingModule } from '@nestjs/testing'
import { HealthController } from './health.controller'

describe('HealthController', () => {
let controller: HealthController;
let controller: HealthController

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [HealthController],
}).compile();
controllers: [HealthController]
}).compile()

controller = module.get<HealthController>(HealthController);
});
controller = module.get<HealthController>(HealthController)
})

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
expect(controller).toBeDefined()
})

it('should return a health check', () => {
expect(controller.getHealth()).toEqual({ status: 'OK' })
})
})
33 changes: 32 additions & 1 deletion packages/core/manifest/src/logger/logger.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import { Test, TestingModule } from '@nestjs/testing'
import { LoggerService } from './logger.service'
import { ConfigService } from '@nestjs/config'

describe('LoggerService', () => {
let service: LoggerService
let originalConsoleLog

const port = 3000

beforeAll(() => {
originalConsoleLog = console.log
console.log = jest.fn()
})

afterAll(() => {
console.log = originalConsoleLog
})

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [LoggerService]
providers: [
LoggerService,
{
provide: ConfigService,
useValue: {
get: jest.fn().mockReturnValue(port)
}
}
]
}).compile()

service = module.get<LoggerService>(LoggerService)
Expand All @@ -15,4 +36,14 @@ describe('LoggerService', () => {
it('should be defined', () => {
expect(service).toBeDefined()
})

it('should console log the URL of the admin panel', () => {
const consoleLogSpy = jest.spyOn(console, 'log')

service.initMessage()

expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining(`http://localhost:${port}`)
)
})
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { Test, TestingModule } from '@nestjs/testing'
import { ManifestController } from './manifest.controller'
import { ManifestService } from '../services/manifest/manifest.service'
import { AuthService } from '../../auth/auth.service'

describe('ManifestController', () => {
let controller: ManifestController

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ManifestController]
controllers: [ManifestController],
providers: [
{
provide: ManifestService,
useValue: {
getAppManifest: jest.fn(),
getEntityManifest: jest.fn()
}
},
{
provide: AuthService,
useValue: {
getUserFromRequest: jest.fn()
}
}
]
}).compile()

controller = module.get<ManifestController>(ManifestController)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { Test, TestingModule } from '@nestjs/testing'
import { ManifestService } from './manifest.service'
import { YamlService } from '../yaml/yaml.service'
import { SchemaService } from '../schema/schema.service'

describe('ManifestService', () => {
let service: ManifestService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ManifestService]
providers: [
ManifestService,
{
provide: YamlService,
useValue: {
load: jest.fn()
}
},
{
provide: SchemaService,
useValue: {
validate: jest.fn()
}
}
]
}).compile()

service = module.get<ManifestService>(ManifestService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ describe('YamlService', () => {
it('should be defined', () => {
expect(service).toBeDefined()
})

it('should load the manifest from the YAML file and transform it into a AppManifest object', () => {
const manifest = service.load()

expect(manifest).toBeDefined()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ import * as fs from 'fs'
import * as yaml from 'js-yaml'

import { AppManifestSchema } from '@mnfst/types'
import { ConfigService } from '@nestjs/config'
import { MANIFEST_FILE_NAME, MANIFEST_FOLDER_NAME } from '../../../constants'

@Injectable()
export class YamlService {
yamlConfig: Object

constructor(private configService: ConfigService) {}

/**
*
* Load the manifest from the YML file and transform it into a AppManifest object.
Expand All @@ -21,7 +16,6 @@ export class YamlService {
*
**/
load(): AppManifestSchema {
this.yamlConfig = this.configService.get('yaml')
let fileContent: string = fs.readFileSync(
`${process.cwd()}/${MANIFEST_FOLDER_NAME}/${MANIFEST_FILE_NAME}`,
'utf8'
Expand Down

0 comments on commit 3077fab

Please sign in to comment.