1- import { describe , beforeEach , afterEach , it } from './../../test-runner/module/index.js' ;
2- import { deepStrictEqual , ok } from 'node:assert' ;
3- import { runSeed } from '../src/db-seed/seed.js' ;
4- import { customers } from '../src/db-seed/customers.js' ;
5- import { initializeServer } from '../src/index.js' ;
1+ import { describe , beforeEach , afterEach , it } from './../../test-runner/module/index.js'
2+ import { deepStrictEqual , ok } from 'node:assert'
3+ import { runSeed } from '../src/db-seed/seed.js'
4+ import { customers } from '../src/db-seed/customers.js'
5+ import { initializeServer } from '../src/index.js'
66
7- import CustomerUtil from './util/customer/customerUtil.js' ;
7+ import CustomerUtil from './util/customer/customerUtil.js'
88
99describe ( 'API Workflow' , ( ) => {
1010 /** @type {import('node:http').Server } _testServer */
11- let _testServer = null ;
11+ let _testServer = null
1212
1313 const customerUtil = new CustomerUtil ( )
1414
1515 beforeEach ( async ( ) => {
16- _testServer = await initializeServer ( ) ;
16+ _testServer = await initializeServer ( )
1717
1818 await new Promise ( ( resolve , reject ) => _testServer . listen (
1919 0 ,
2020 err => err ? reject ( err ) : resolve ( )
21- ) ) ;
22- const { port } = _testServer . address ( ) ;
23- customerUtil . setContextURL ( `http://localhost:${ port } ` ) ;
24- await runSeed ( ) ;
25- } ) ;
21+ ) )
22+ const { port } = _testServer . address ( )
23+ customerUtil . setContextURL ( `http://localhost:${ port } ` )
24+ await runSeed ( )
25+ } )
2626
2727 afterEach ( async ( ) => {
2828 await new Promise ( resolve => _testServer . close ( resolve ) )
2929 customerUtil . setContextURL ( '' )
30- } ) ;
30+ } )
3131
3232 it ( 'should create customer' , async ( ) => {
3333 const input = {
3434 name : 'Xuxa da Silva' ,
3535 phone : '123456789' ,
36- } ;
36+ }
3737
3838 // Check if customer does not exist before creation
39- const { result : customersBefore } = await customerUtil . getCustomers ( `?name=${ input . name } &phone=${ input . phone } ` ) ;
40- deepStrictEqual ( customersBefore . length , 0 ) ;
39+ const { result : customersBefore } = await customerUtil . getCustomers ( `?name=${ input . name } &phone=${ input . phone } ` )
40+ deepStrictEqual ( customersBefore . length , 0 )
4141
42- const expected = { message : `customer ${ input . name } created!` } ;
42+ const expected = { message : `customer ${ input . name } created!` }
4343
44- const { body, statusCode } = await customerUtil . createCustomer ( input ) ;
44+ const { body, statusCode } = await customerUtil . createCustomer ( input )
4545 ok ( body . _id )
46- deepStrictEqual ( body . message , expected . message ) ;
47- deepStrictEqual ( statusCode , 201 ) ;
46+ deepStrictEqual ( body . message , expected . message )
47+ deepStrictEqual ( statusCode , 201 )
4848
4949 // Check if customer exists after creation
50- const { result : customersAfter } = await customerUtil . getCustomers ( `?name=${ input . name } &phone=${ input . phone } ` ) ;
51- deepStrictEqual ( customersAfter . length , 1 ) ;
52- deepStrictEqual ( customersAfter [ 0 ] . name , input . name ) ;
53- deepStrictEqual ( customersAfter [ 0 ] . phone , input . phone ) ;
54- } ) ;
50+ const { result : customersAfter } = await customerUtil . getCustomers ( `?name=${ input . name } &phone=${ input . phone } ` )
51+ deepStrictEqual ( customersAfter . length , 1 )
52+ deepStrictEqual ( customersAfter [ 0 ] . name , input . name )
53+ deepStrictEqual ( customersAfter [ 0 ] . phone , input . phone )
54+ } )
5555
5656
5757 it ( 'should retrieve only initial customers' , async ( ) => {
58- return customerUtil . validateCustomersListOrderedByName ( customers ) ;
59- } ) ;
58+ return customerUtil . validateCustomersListOrderedByName ( customers )
59+ } )
6060
6161 it ( 'given 5 different customers it should have valid list' , async ( ) => {
6262 const customersToInsert = [
@@ -65,68 +65,68 @@ describe('API Workflow', () => {
6565 { name : 'Shrek de Souza' , phone : '3333333333' } ,
6666 { name : 'Nemo de Oliveira' , phone : '4444444444' } ,
6767 { name : 'Buzz da Rocha' , phone : '5555555555' } ,
68- ] ;
68+ ]
6969
70- await Promise . all ( customersToInsert . map ( item => customerUtil . createCustomer ( item ) ) ) ;
71- await customerUtil . validateCustomersListOrderedByName ( customers . concat ( customersToInsert ) ) ;
72- } ) ;
70+ await Promise . all ( customersToInsert . map ( item => customerUtil . createCustomer ( item ) ) )
71+ await customerUtil . validateCustomersListOrderedByName ( customers . concat ( customersToInsert ) )
72+ } )
7373
7474 it ( 'should filter customers by name' , async ( ) => {
7575 const { name } = customers . at ( 0 )
76- const { statusCode, result } = await customerUtil . getCustomers ( `?name=${ name } ` ) ;
76+ const { statusCode, result } = await customerUtil . getCustomers ( `?name=${ name } ` )
7777 const { _id, ...output } = result . at ( 0 )
7878 ok ( _id )
7979
80- deepStrictEqual ( statusCode , 200 ) ;
81- deepStrictEqual ( output , customers . find ( customer => customer . name === name ) ) ;
82- } ) ;
80+ deepStrictEqual ( statusCode , 200 )
81+ deepStrictEqual ( output , customers . find ( customer => customer . name === name ) )
82+ } )
8383
8484 it ( 'should filter customers by phone' , async ( ) => {
8585 const { phone } = customers . at ( 1 )
86- const { statusCode, result } = await customerUtil . getCustomers ( `?phone=${ phone } ` ) ;
86+ const { statusCode, result } = await customerUtil . getCustomers ( `?phone=${ phone } ` )
8787 const { _id, ...output } = result . at ( 0 )
8888 ok ( _id )
8989
90- deepStrictEqual ( statusCode , 200 ) ;
91- deepStrictEqual ( output , customers . find ( customer => customer . phone === phone ) ) ;
92- } ) ;
90+ deepStrictEqual ( statusCode , 200 )
91+ deepStrictEqual ( output , customers . find ( customer => customer . phone === phone ) )
92+ } )
9393
9494 it ( 'should update customer' , async ( ) => {
9595 const input = {
9696 name : 'Xuxa da Silva' ,
9797 phone : '123456789' ,
98- } ;
99- const { body : { _id } } = await customerUtil . createCustomer ( input ) ;
98+ }
99+ const { body : { _id } } = await customerUtil . createCustomer ( input )
100100
101- const updateData = { phone : '987654321' } ;
102- const expected = { message : `customer ${ _id } updated!` } ;
101+ const updateData = { phone : '987654321' }
102+ const expected = { message : `customer ${ _id } updated!` }
103103
104- const { body, statusCode } = await customerUtil . updateCustomer ( _id , updateData ) ;
105- deepStrictEqual ( body , expected ) ;
106- deepStrictEqual ( statusCode , 200 ) ;
104+ const { body, statusCode } = await customerUtil . updateCustomer ( _id , updateData )
105+ deepStrictEqual ( body , expected )
106+ deepStrictEqual ( statusCode , 200 )
107107
108108 // Check if customer updated successfully
109- const { result : updatedCustomers } = await customerUtil . getCustomers ( `?name=${ input . name } &phone=${ updateData . phone } ` ) ;
110- deepStrictEqual ( updatedCustomers . length , 1 ) ;
111- deepStrictEqual ( updatedCustomers [ 0 ] . phone , updateData . phone ) ;
112- deepStrictEqual ( updatedCustomers [ 0 ] . name , input . name ) ;
113- } ) ;
109+ const { result : updatedCustomers } = await customerUtil . getCustomers ( `?name=${ input . name } &phone=${ updateData . phone } ` )
110+ deepStrictEqual ( updatedCustomers . length , 1 )
111+ deepStrictEqual ( updatedCustomers [ 0 ] . phone , updateData . phone )
112+ deepStrictEqual ( updatedCustomers [ 0 ] . name , input . name )
113+ } )
114114
115115 it ( 'should delete customer' , async ( ) => {
116116 const input = {
117117 name : 'Xuxa da Silva' ,
118118 phone : '123456789' ,
119- } ;
120- const { body : { _id } } = await customerUtil . createCustomer ( input ) ;
119+ }
120+ const { body : { _id } } = await customerUtil . createCustomer ( input )
121121
122- const expected = { message : `customer ${ _id } deleted!` } ;
122+ const expected = { message : `customer ${ _id } deleted!` }
123123
124- const { body, statusCode } = await customerUtil . deleteCustomer ( _id ) ;
125- deepStrictEqual ( body , expected ) ;
126- deepStrictEqual ( statusCode , 200 ) ;
124+ const { body, statusCode } = await customerUtil . deleteCustomer ( _id )
125+ deepStrictEqual ( body , expected )
126+ deepStrictEqual ( statusCode , 200 )
127127
128128 // Check if customer deleted successfully
129- const { result : remainingCustomers } = await customerUtil . getCustomers ( `?name=${ input . name } &phone=${ input . phone } ` ) ;
130- deepStrictEqual ( remainingCustomers . length , 0 ) ;
131- } ) ;
132- } ) ;
129+ const { result : remainingCustomers } = await customerUtil . getCustomers ( `?name=${ input . name } &phone=${ input . phone } ` )
130+ deepStrictEqual ( remainingCustomers . length , 0 )
131+ } )
132+ } )
0 commit comments