-
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.
Merge pull request #87 from DavianYang/feature/customer
Feature/customer
- Loading branch information
Showing
15 changed files
with
421 additions
and
265 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,6 +1,3 @@ | ||
overwrite: true | ||
schema: | ||
- 'apps/core/src/schema.gql' | ||
documents: | ||
- '*.spec.ts' | ||
- 'libs/**/src/lib/*.spec.ts' | ||
- 'apps/core/src/schema.gql' |
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
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,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); | ||
}); | ||
}); |
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
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,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'; |
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 |
---|---|---|
@@ -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', | ||
}, | ||
], | ||
}; |
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
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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
12
libs/nsx-testing/src/lib/population/populate-for-testing.ts
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 |
---|---|---|
@@ -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); | ||
} |
3 changes: 3 additions & 0 deletions
3
libs/nsx-testing/src/lib/population/populate-options.interface.ts
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface PopulateOptions { | ||
customerCount?: number; | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.