Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Erick Wendel <erick.workspace@gmail.com>
  • Loading branch information
ErickWendel committed Dec 7, 2024
1 parent 2b16b1b commit 5cecea2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 74 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

- name: Set up Docker Compose
run: |
sudo rm /usr/local/bin/docker-compose
sudo rm -f /usr/local/bin/docker-compose
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /tmp/docker-compose
chmod +x /tmp/docker-compose
sudo mv /tmp/docker-compose /usr/local/bin/docker-compose
Expand All @@ -32,7 +32,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22

- name: Restore dependencies
run: |
Expand Down
4 changes: 2 additions & 2 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"mongodb": "^6.7.0"
},
"engines": {
"node": "v20.12.1"
"node": "v22.11.0"
}
}
}
124 changes: 62 additions & 62 deletions app/test/api.test.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
import { describe, beforeEach, afterEach, it } from './../../test-runner/module/index.js';
import { deepStrictEqual, ok } from 'node:assert';
import { runSeed } from '../src/db-seed/seed.js';
import { customers } from '../src/db-seed/customers.js';
import { initializeServer } from '../src/index.js';
import { describe, beforeEach, afterEach, it } from './../../test-runner/module/index.js'
import { deepStrictEqual, ok } from 'node:assert'
import { runSeed } from '../src/db-seed/seed.js'
import { customers } from '../src/db-seed/customers.js'
import { initializeServer } from '../src/index.js'

import CustomerUtil from './util/customer/customerUtil.js';
import CustomerUtil from './util/customer/customerUtil.js'

describe('API Workflow', () => {
/** @type {import('node:http').Server} _testServer */
let _testServer = null;
let _testServer = null

const customerUtil = new CustomerUtil()

beforeEach(async () => {
_testServer = await initializeServer();
_testServer = await initializeServer()

await new Promise((resolve, reject) => _testServer.listen(
0,
err => err ? reject(err) : resolve()
));
const { port } = _testServer.address();
customerUtil.setContextURL(`http://localhost:${port}`);
await runSeed();
});
))
const { port } = _testServer.address()
customerUtil.setContextURL(`http://localhost:${port}`)
await runSeed()
})

afterEach(async () => {
await new Promise(resolve => _testServer.close(resolve))
customerUtil.setContextURL('')
});
})

it('should create customer', async () => {
const input = {
name: 'Xuxa da Silva',
phone: '123456789',
};
}

// Check if customer does not exist before creation
const { result: customersBefore } = await customerUtil.getCustomers(`?name=${input.name}&phone=${input.phone}`);
deepStrictEqual(customersBefore.length, 0);
const { result: customersBefore } = await customerUtil.getCustomers(`?name=${input.name}&phone=${input.phone}`)
deepStrictEqual(customersBefore.length, 0)

const expected = { message: `customer ${input.name} created!` };
const expected = { message: `customer ${input.name} created!` }

const { body, statusCode } = await customerUtil.createCustomer(input);
const { body, statusCode } = await customerUtil.createCustomer(input)
ok(body._id)
deepStrictEqual(body.message, expected.message);
deepStrictEqual(statusCode, 201);
deepStrictEqual(body.message, expected.message)
deepStrictEqual(statusCode, 201)

// Check if customer exists after creation
const { result: customersAfter } = await customerUtil.getCustomers(`?name=${input.name}&phone=${input.phone}`);
deepStrictEqual(customersAfter.length, 1);
deepStrictEqual(customersAfter[0].name, input.name);
deepStrictEqual(customersAfter[0].phone, input.phone);
});
const { result: customersAfter } = await customerUtil.getCustomers(`?name=${input.name}&phone=${input.phone}`)
deepStrictEqual(customersAfter.length, 1)
deepStrictEqual(customersAfter[0].name, input.name)
deepStrictEqual(customersAfter[0].phone, input.phone)
})


it('should retrieve only initial customers', async () => {
return customerUtil.validateCustomersListOrderedByName(customers);
});
return customerUtil.validateCustomersListOrderedByName(customers)
})

it('given 5 different customers it should have valid list', async () => {
const customersToInsert = [
Expand All @@ -65,68 +65,68 @@ describe('API Workflow', () => {
{ name: 'Shrek de Souza', phone: '3333333333' },
{ name: 'Nemo de Oliveira', phone: '4444444444' },
{ name: 'Buzz da Rocha', phone: '5555555555' },
];
]

await Promise.all(customersToInsert.map(item => customerUtil.createCustomer(item)));
await customerUtil.validateCustomersListOrderedByName(customers.concat(customersToInsert));
});
await Promise.all(customersToInsert.map(item => customerUtil.createCustomer(item)))
await customerUtil.validateCustomersListOrderedByName(customers.concat(customersToInsert))
})

it('should filter customers by name', async () => {
const { name } = customers.at(0)
const { statusCode, result } = await customerUtil.getCustomers(`?name=${name}`);
const { statusCode, result } = await customerUtil.getCustomers(`?name=${name}`)
const { _id, ...output } = result.at(0)
ok(_id)

deepStrictEqual(statusCode, 200);
deepStrictEqual(output, customers.find(customer => customer.name === name));
});
deepStrictEqual(statusCode, 200)
deepStrictEqual(output, customers.find(customer => customer.name === name))
})

it('should filter customers by phone', async () => {
const { phone } = customers.at(1)
const { statusCode, result } = await customerUtil.getCustomers(`?phone=${phone}`);
const { statusCode, result } = await customerUtil.getCustomers(`?phone=${phone}`)
const { _id, ...output } = result.at(0)
ok(_id)

deepStrictEqual(statusCode, 200);
deepStrictEqual(output, customers.find(customer => customer.phone === phone));
});
deepStrictEqual(statusCode, 200)
deepStrictEqual(output, customers.find(customer => customer.phone === phone))
})

it('should update customer', async () => {
const input = {
name: 'Xuxa da Silva',
phone: '123456789',
};
const { body: { _id } } = await customerUtil.createCustomer(input);
}
const { body: { _id } } = await customerUtil.createCustomer(input)

const updateData = { phone: '987654321' };
const expected = { message: `customer ${_id} updated!` };
const updateData = { phone: '987654321' }
const expected = { message: `customer ${_id} updated!` }

const { body, statusCode } = await customerUtil.updateCustomer(_id, updateData);
deepStrictEqual(body, expected);
deepStrictEqual(statusCode, 200);
const { body, statusCode } = await customerUtil.updateCustomer(_id, updateData)
deepStrictEqual(body, expected)
deepStrictEqual(statusCode, 200)

// Check if customer updated successfully
const { result: updatedCustomers } = await customerUtil.getCustomers(`?name=${input.name}&phone=${updateData.phone}`);
deepStrictEqual(updatedCustomers.length, 1);
deepStrictEqual(updatedCustomers[0].phone, updateData.phone);
deepStrictEqual(updatedCustomers[0].name, input.name);
});
const { result: updatedCustomers } = await customerUtil.getCustomers(`?name=${input.name}&phone=${updateData.phone}`)
deepStrictEqual(updatedCustomers.length, 1)
deepStrictEqual(updatedCustomers[0].phone, updateData.phone)
deepStrictEqual(updatedCustomers[0].name, input.name)
})

it('should delete customer', async () => {
const input = {
name: 'Xuxa da Silva',
phone: '123456789',
};
const { body: { _id } } = await customerUtil.createCustomer(input);
}
const { body: { _id } } = await customerUtil.createCustomer(input)

const expected = { message: `customer ${_id} deleted!` };
const expected = { message: `customer ${_id} deleted!` }

const { body, statusCode } = await customerUtil.deleteCustomer(_id);
deepStrictEqual(body, expected);
deepStrictEqual(statusCode, 200);
const { body, statusCode } = await customerUtil.deleteCustomer(_id)
deepStrictEqual(body, expected)
deepStrictEqual(statusCode, 200)

// Check if customer deleted successfully
const { result: remainingCustomers } = await customerUtil.getCustomers(`?name=${input.name}&phone=${input.phone}`);
deepStrictEqual(remainingCustomers.length, 0);
});
});
const { result: remainingCustomers } = await customerUtil.getCustomers(`?name=${input.name}&phone=${input.phone}`)
deepStrictEqual(remainingCustomers.length, 0)
})
})
18 changes: 10 additions & 8 deletions test-runner/bin/test-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ class TestRunner {
events.testPass,
events.testFail
];

const shouldNotCount = (event, name) => (
hooksNotPrint.includes(name)
&&
event !== events.log
)
cp.on('message', ({ event, data }) => {
if (!event) return;

Expand All @@ -48,6 +52,7 @@ class TestRunner {
this.tests.suites++;
break;
case events.testPass:
if (shouldNotCount(event, data.name)) break;
this.tests.passing++;
break;
case events.testFail:
Expand All @@ -57,26 +62,23 @@ class TestRunner {
break;
}

if (
hooksNotPrint.includes(data.name)
&&
event !== events.log
) return;
if (shouldNotCount(event, data.name)) return;

if (!eventsOutput.includes(event)) return;

const formattedResult = this.formatter.formatTestResult(data, event);
this.results.set(data.tree, this.results.get(data.tree) || []);
this.results.get(data.tree).push(formattedResult);
this.reporter.updateOutput(this.results, this.formatter);
});


cp.once('error', (err) => {
console.error(err)
})

return new Promise(resolve => cp.once('exit', resolve))
return new Promise(resolve => cp.once('exit', resolve)).finally(() => {
this.reporter.updateOutput(this.results, this.formatter);
})
}
}

Expand Down

0 comments on commit 5cecea2

Please sign in to comment.