Skip to content

Commit

Permalink
[#29] Add tests for AWS templates
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangmirs committed Jul 26, 2022
1 parent 2b3694d commit d83a963
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
83 changes: 83 additions & 0 deletions src/templates/aws/advanced.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { AwsOptions } from '.';
import { remove } from '../../helpers/file';
import {
applyAlb,
applyBastion,
applyCommon,
applyEcr,
applyEcs,
applyLog,
applyRds,
applyRegion,
applyS3,
applySecurityGroup,
applySsm,
applyVpc,
} from './addons';
import { applyAdvancedTemplate } from './advanced';

jest.mock('./addons');

describe('AWS advanced template', () => {
describe('applyAdvancedTemplate', () => {
const projectDir = 'aws-advanced-test';
const options: AwsOptions = { projectName: projectDir, provider: 'aws', infrastructureType: 'advanced', awsRegion: 'ap-southeast-1' };

beforeAll(() => {
applyAdvancedTemplate(options);
});

afterAll(() => {
jest.clearAllMocks();
remove('/', projectDir);
});

it('applies common add-on', () => {
expect(applyCommon).toHaveBeenCalledWith(options);
});

it('applies region add-on', () => {
expect(applyRegion).toHaveBeenCalledWith(options);
});

it('applies VPC add-on', () => {
expect(applyVpc).toHaveBeenCalledWith(options);
});

it('applies security group add-on', () => {
expect(applySecurityGroup).toHaveBeenCalledWith(options);
});

it('applies ECR add-on', () => {
expect(applyEcr).toHaveBeenCalledWith(options);
});

it('applies log add-on', () => {
expect(applyLog).toHaveBeenCalledWith(options);
});

it('applies S3 add-on', () => {
expect(applyS3).toHaveBeenCalledWith(options);
});

it('applies ALB add-on', () => {
expect(applyAlb).toHaveBeenCalledWith(options);
});

it('applies RDS add-on', () => {
expect(applyRds).toHaveBeenCalledWith(options);
});

it('applies bastion add-on', () => {
expect(applyBastion).toHaveBeenCalledWith(options);
});

it('applies SSM add-on', () => {
expect(applySsm).toHaveBeenCalledWith(options);
});

it('applies ECS add-on', () => {
expect(applyEcs).toHaveBeenCalledWith(options);
});
});
});
15 changes: 14 additions & 1 deletion src/templates/aws/advanced.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { AwsOptions } from '.';
import { applyAlb, applyBastion, applyCommon, applyEcr, applyEcs, applyLog, applyRds, applyRegion, applyS3, applySecurityGroup, applySsm, applyVpc } from './addons';
import {
applyAlb,
applyBastion,
applyCommon,
applyEcr,
applyEcs,
applyLog,
applyRds,
applyRegion,
applyS3,
applySecurityGroup,
applySsm,
applyVpc,
} from './addons';

const applyAdvancedTemplate = (options: AwsOptions) => {
applyCommon(options);
Expand Down
50 changes: 50 additions & 0 deletions src/templates/aws/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { prompt } from 'inquirer';

import { generateAwsTemplate } from '.';
import { GeneralOptions } from '../../commands/generate';
import { remove } from '../../helpers/file';
import { applyAdvancedTemplate } from './advanced';

jest.mock('./advanced');
jest.mock('inquirer');

describe('AWS template', () => {
describe('generateAwsTemplate', () => {
const projectDir = 'aws-advanced-test';
const options: GeneralOptions = { projectName: projectDir, provider: 'aws' };

describe('given infrastructureType is basic', () => {
beforeAll(() => {
(prompt as unknown as jest.Mock)
.mockResolvedValueOnce({ infrastructureType: 'basic' });
});

afterAll(() => {
jest.clearAllMocks();
remove('/', projectDir);
});

it('throws the error message', async() => {
await expect(generateAwsTemplate(options)).rejects.toThrow('This type has not been implemented!');
});
});

describe('given infrastructureType is advanced', () => {
beforeAll(() => {
(prompt as unknown as jest.Mock)
.mockResolvedValueOnce({ infrastructureType: 'advanced' });
});

afterAll(() => {
jest.clearAllMocks();
remove('/', projectDir);
});

it('applies advanced add-ons', async() => {
await generateAwsTemplate(options);

expect(applyAdvancedTemplate).toHaveBeenCalled();
});
});
});
});

0 comments on commit d83a963

Please sign in to comment.