-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1de7810
commit 17aa3ee
Showing
2 changed files
with
65 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
import { AppController } from './app.controller'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { firstValueFrom, of, throwError } from 'rxjs'; | ||
import { of, Subject, Subscription, throwError } from 'rxjs'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { BadRequestException, UnauthorizedException } from '@nestjs/common'; | ||
import { DeploymentInfo } from './deployment-info.dto'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import * as fs from 'fs'; | ||
|
||
let onSubscription: Subscription; | ||
const lines = new Subject<string>(); | ||
jest.mock('tail', () => { | ||
// Mock Tail constructor | ||
return { | ||
Tail: jest.fn().mockReturnValue({ | ||
on: (event, callback) => (onSubscription = lines.subscribe(callback)), | ||
unwatch: () => onSubscription.unsubscribe(), | ||
}), | ||
}; | ||
}); | ||
|
||
describe('AppController', () => { | ||
let controller: AppController; | ||
let mockHttp: { post: jest.Mock }; | ||
let mockWs: { write: jest.Mock; close: jest.Mock }; | ||
|
||
const deploymentData: DeploymentInfo = { | ||
name: 'test-name', | ||
locale: 'de', | ||
|
@@ -23,6 +37,8 @@ describe('AppController', () => { | |
}; | ||
|
||
beforeEach(async () => { | ||
mockWs = { write: jest.fn(), close: jest.fn() }; | ||
jest.spyOn(fs, 'createWriteStream').mockReturnValue(mockWs as any); | ||
mockHttp = { | ||
post: jest.fn().mockReturnValue(of({ data: undefined })), | ||
}; | ||
|
@@ -63,33 +79,57 @@ describe('AppController', () => { | |
}); | ||
}); | ||
|
||
it('should write arguments to file', async () => { | ||
const mockWs = { write: jest.fn(), close: jest.fn() }; | ||
jest.spyOn(fs, 'createWriteStream').mockReturnValue(mockWs as any); | ||
it('should throw error if ERROR is written to log', (done) => { | ||
controller.deployApp(deploymentData).subscribe({ | ||
error: (err: BadRequestException) => { | ||
expect(err).toBeInstanceOf(BadRequestException); | ||
expect(err.message).toBe('my custom error'); | ||
// Ensure tail is properly "unwatched" | ||
expect(onSubscription.closed).toBeTruthy(); | ||
done(); | ||
}, | ||
}); | ||
|
||
await firstValueFrom(controller.deployApp(deploymentData)); | ||
lines.next('some logs'); | ||
lines.next('ERROR my custom error'); | ||
}); | ||
|
||
expect(mockWs.write).toHaveBeenCalledWith( | ||
'test-name de [email protected] test-username test-base y n', | ||
); | ||
expect(mockWs.close).toHaveBeenCalled(); | ||
it('should write arguments to file', (done) => { | ||
controller.deployApp(deploymentData).subscribe(() => { | ||
expect(mockWs.write).toHaveBeenCalledWith( | ||
'test-name de [email protected] test-username test-base y n', | ||
); | ||
expect(mockWs.close).toHaveBeenCalled(); | ||
// Ensure tail is properly "unwatched" | ||
expect(onSubscription.closed).toBeTruthy(); | ||
done(); | ||
}); | ||
|
||
lines.next('DONE'); | ||
}); | ||
|
||
it('should use the default locale if empty or omitted', async () => { | ||
const mockWs = { write: jest.fn(), close: jest.fn() }; | ||
jest.spyOn(fs, 'createWriteStream').mockReturnValue(mockWs as any); | ||
it('should use the default locale if empty', (done) => { | ||
const emptyLocale = { ...deploymentData, locale: '' }; | ||
controller.deployApp(emptyLocale).subscribe(() => { | ||
expect(mockWs.write).toHaveBeenCalledWith( | ||
'test-name en [email protected] test-username test-base y n', | ||
); | ||
done(); | ||
}); | ||
|
||
const withoutLocale = { ...deploymentData, locale: '' }; | ||
await firstValueFrom(controller.deployApp(withoutLocale)); | ||
expect(mockWs.write).toHaveBeenCalledWith( | ||
'test-name en [email protected] test-username test-base y n', | ||
); | ||
lines.next('DONE'); | ||
}); | ||
|
||
mockWs.write.mockReset(); | ||
delete withoutLocale.locale; | ||
await firstValueFrom(controller.deployApp(withoutLocale)); | ||
expect(mockWs.write).toHaveBeenCalledWith( | ||
'test-name en [email protected] test-username test-base y n', | ||
); | ||
it('should use the default locale if omitted', (done) => { | ||
const noLocale = { ...deploymentData }; | ||
delete noLocale.locale; | ||
controller.deployApp(noLocale).subscribe(() => { | ||
expect(mockWs.write).toHaveBeenCalledWith( | ||
'test-name en [email protected] test-username test-base y n', | ||
); | ||
done(); | ||
}); | ||
|
||
lines.next('DONE'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters