Skip to content

Commit

Permalink
Merge pull request #87 from DavianYang/feature/customer
Browse files Browse the repository at this point in the history
Feature/customer
  • Loading branch information
DavianYang authored Mar 26, 2022
2 parents 63fabdb + 1f938e8 commit 6659f80
Show file tree
Hide file tree
Showing 15 changed files with 421 additions and 265 deletions.
5 changes: 1 addition & 4 deletions codegen.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
overwrite: true
schema:
- 'apps/core/src/schema.gql'
documents:
- '*.spec.ts'
- 'libs/**/src/lib/*.spec.ts'
- 'apps/core/src/schema.gql'
7 changes: 5 additions & 2 deletions libs/nsx-customer/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ module.exports = {
},
},
transform: {
'^.+\\.[tj]s$': 'ts-jest',
},
"node_modules/variables/.+\\.(j|t)sx?$": "ts-jest"
},
transformIgnorePatterns: [
"node_modules/(?!variables/.*)"
],
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/nsx-customer',
};
76 changes: 71 additions & 5 deletions libs/nsx-customer/src/lib/customer.resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,93 @@
import { PrismaModule, PrismaService } from '@myancommerce/nsx-prisma';
import { testConfiguration, testEnvironment } from '@myancommerce/nsx-testing';
import { ConfigModule } from '@nestjs/config';
import { gql } from 'apollo-server-express';

import { ConfigModule, ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';

import { PrismaModule, PrismaService } from '@myancommerce/nsx-prisma';
import {
populateForTesting,
testConfiguration,
testEnvironment,
TestGraphQLClient,
} from '@myancommerce/nsx-testing';

import { CustomerModule } from './customer.module';
import { GraphQLModule } from '@nestjs/graphql';
import { INestApplication } from '@nestjs/common';
import { RoleModule } from '@myancommerce/nsx-role';

const GET_CUSTOMERS = gql`
query GetCustomers {
customers {
id
firstName
lastName
title
emailAddress
phoneNumber
}
}
`;

describe('CustomerResolver', () => {
let app: INestApplication;
let prisma: PrismaService;
let apolloClient: TestGraphQLClient;

beforeEach(async () => {
beforeAll(async () => {
const moduleRef: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
...testEnvironment.appConfig,
load: [testConfiguration],
}),
GraphQLModule.forRootAsync({
useFactory: async () => ({
...testEnvironment.graphqlConfig,
path: testEnvironment.apiConfig.adminApiPath,
debug: testEnvironment.apiConfig.adminApiDebug,
playground:
testEnvironment.apiConfig.adminApiPlayground,
context: ({ req, res }: any) => ({
request: req,
response: res,
}),
}),
}),
PrismaModule,
CustomerModule,
RoleModule,
],
}).compile();

prisma = moduleRef.get(PrismaService);
await prisma.cleanDatabase();

const configService = moduleRef.get(ConfigService);

app = moduleRef.createNestApplication();

await app.listen(
configService.get('apiConfig.port') as number,
(configService.get('apiConfig.hostname') as string) || '',
);

await populateForTesting(app, { customerCount: 5 });

apolloClient = new TestGraphQLClient(
`http://localhost:${configService.get(
'apiConfig.port',
)}/${configService.get('apiConfig.adminApiPath')}`,
);
});

it('should be defined', () => {});
afterAll(async () => {
await app.close();
});

it('it get customers', async () => {
let result = await apolloClient.query(GET_CUSTOMERS);

expect(result.customers.length).toBe(5);
});
});
2 changes: 1 addition & 1 deletion libs/nsx-customer/src/lib/customer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CustomerService } from './customer.service';
describe('CustomerService', () => {
let service: CustomerService;

beforeEach(async () => {
beforeAll(async () => {
const moduleRef: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
Expand Down
13 changes: 13 additions & 0 deletions libs/nsx-testing/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
// Test Config
export * from './lib/test.config';

// Test Environment
export * from './lib/environments/test-environment';

// Test Graphql Client
export * from './lib/test-graphql-client';

// Population
export * from './lib/population/mock-data.service';
export * from './lib/population/customers.populate';
export * from './lib/population/roles.populate';
export * from './lib/population/populate-for-testing';
export * from './lib/population/populate-options.interface';
12 changes: 12 additions & 0 deletions libs/nsx-testing/src/lib/data/e2e-initial-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const initialData = {
roles: [
{
code: '__customer_role__',
description: 'This is test customer Role',
},
{
code: '__super_admin_role__',
description: 'This is test customer Role',
},
],
};
2 changes: 1 addition & 1 deletion libs/nsx-testing/src/lib/environments/test-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const appConfig: ConfigModuleOptions = {

const apiConfig: ApiOptions = {
hostname: process.env['DATABASE_HOST'],
port: 3000,
port: 3200,

adminApiPath: 'admin-api',
adminApiPlayground: true, // turn this off for production
Expand Down
16 changes: 16 additions & 0 deletions libs/nsx-testing/src/lib/population/customers.populate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { INestApplication } from '@nestjs/common';
import { CustomerService } from '@myancommerce/nsx-customer';
import { MockDataService } from './mock-data.service';

export async function populateCustomers(app: INestApplication, count: number) {
const customerService = app.get(CustomerService);
const customerData = MockDataService.getCustomers(count);

for (const { customer } of customerData) {
try {
await customerService.create(customer);
} catch (e) {
console.log(e);
}
}
}
28 changes: 28 additions & 0 deletions libs/nsx-testing/src/lib/population/mock-data.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { faker } from '@faker-js/faker';
import { CreateCustomerInput } from '@myancommerce/nsx-customer';

export class MockDataService {
constructor() {
faker.seed();
}

static getCustomers(
count: number,
): Array<{ customer: CreateCustomerInput }> {
const results: Array<{ customer: CreateCustomerInput }> = [];

for (let i = 0; i < count; i++) {
const customer: CreateCustomerInput = {
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
phoneNumber: faker.phone.phoneNumber(),
emailAddress: faker.internet.email(),
title: faker.fake('Good Morning {{name.firstName}}!'),
};

results.push({ customer });
}

return results;
}
}
12 changes: 12 additions & 0 deletions libs/nsx-testing/src/lib/population/populate-for-testing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { INestApplication } from '@nestjs/common';
import { populateCustomers } from './customers.populate';
import { PopulateOptions } from './populate-options.interface';
import { populateRoles } from './roles.populate';

export async function populateForTesting(
app: INestApplication,
options: PopulateOptions,
): Promise<void> {
await populateRoles(app);
await populateCustomers(app, options.customerCount ?? 1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface PopulateOptions {
customerCount?: number;
}
16 changes: 16 additions & 0 deletions libs/nsx-testing/src/lib/population/roles.populate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { RoleService } from '@myancommerce/nsx-role';
import { INestApplication } from '@nestjs/common';
import { initialData } from '../data/e2e-initial-data';

export async function populateRoles(app: INestApplication) {
const roleService = app.get(RoleService);
const roleData = initialData.roles;

for (const role of roleData) {
try {
await roleService.createRole({ data: role });
} catch (e) {
console.log(e);
}
}
}
6 changes: 3 additions & 3 deletions libs/nsx-testing/src/lib/test-graphql-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export class TestGraphQLClient {
private headers: { [key: string]: any } = {};
constructor(private apiUrl: string = '') {}

async query<T = any, V = Record<string, any>>(
async query(
query: DocumentNode,
variables?: V,
variables?: any,
queryParams?: QueryParams,
): Promise<T> {
): Promise<any> {
const response = await this.makeGraphQLRequest(
query,
variables,
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@changesets/cli": "^2.21.1",
"@commitlint/cli": "^16.2.1",
"@commitlint/config-conventional": "^16.2.1",
"@faker-js/faker": "^6.0.0",
"@graphql-codegen/cli": "2.6.2",
"@graphql-codegen/introspection": "2.1.1",
"@graphql-codegen/typescript": "2.4.5",
Expand All @@ -73,13 +74,12 @@
"@nrwl/nx-cloud": "latest",
"@nrwl/tao": "13.8.3",
"@nrwl/workspace": "13.8.3",
"@types/chance": "^1.1.3",
"@types/jest": "^27.4.1",
"@types/node-fetch": "2",
"@types/pg": "^8.6.5",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"apollo-server": "^3.6.3",
"chance": "^1.1.8",
"child_process": "^1.0.2",
"concurrently": "^6.5.1",
"cross-env": "^7.0.3",
Expand All @@ -97,9 +97,9 @@
"graphql-request": "^4.0.0",
"graphql-tag": "^2.12.6",
"graphql-tools": "^8.2.0",
"jest": "^27.5.1",
"jest": "27.4.7",
"lint-staged": "^11.2.6",
"node-fetch": "^3.2.2",
"node-fetch": "2",
"prettier": "^2.5.1",
"prisma": "^3.10.0",
"prisma-dbml-generator": "^0.8.3",
Expand Down
Loading

0 comments on commit 6659f80

Please sign in to comment.