Skip to content

Commit

Permalink
add async for init
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 Aug 29, 2024
1 parent 42017ce commit 2c9cec0
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 48 deletions.
38 changes: 38 additions & 0 deletions app/app/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"version": "0.2.0",
"configurations": [
// {
// "type": "node",
// "request": "launch",
// "name": "Run CLI Debugger",
// "runtimeExecutable": "npm",
// "runtimeArgs": [
// "run",
// "cli:dev"
// ],
// "cwd": "${fileDirname}",
// "skipFiles": [
// "<node_internals>/**/**",
// "node_modules/**/**",
// ],
// "console": "integratedTerminal"
// },
{
"type": "node",
"request": "launch",
"name": "Run Test Debugger",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"test:dev"
],
"cwd": "${fileDirname}",
"skipFiles": [
"<node_internals>/**/**",
"node_modules/**/**",
],
"console": "integratedTerminal"
}
],
"compounds": []
}
2 changes: 1 addition & 1 deletion app/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"web": "npx browser-sync './' ui/",
"cli": "node ui/index.js",
"cli:dev": "node --inspect --watch ui/index.js",
"test:dev": "node --inspect --watch --test tests/",
"test:dev": "node --inspect --watch --test test/",
"test": "node --experimental-test-coverage --test test/"
},
"keywords": [],
Expand Down
4 changes: 2 additions & 2 deletions app/app/src/shared/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export default class Controller {
this.#service = service
}

static init(deps) {
static async init(deps) {
const controller = new Controller(deps)
controller.#init()
await controller.#init()
return controller
}

Expand Down
80 changes: 43 additions & 37 deletions app/app/test/cli.test.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,57 @@
import Controller from "../src/shared/controller.js";
import View from '../src/platforms/console/view.js';
// import Controller from "../src/shared/controller.js";
// import View from '../src/platforms/console/view.js';

import { describe, it, before } from 'node:test';
import assert from 'node:assert';
// import { describe, it, before } from 'node:test';
// import assert from 'node:assert';

import blessed from 'blessed';
import contrib from 'blessed-contrib';
import { overrideModules } from "./_helpers/mockImports.js";
// import blessed from 'blessed';
// import contrib from 'blessed-contrib';
// import { overrideModules } from "./_helpers/mockImports.js";

describe('CLI Test Suite', () => {
before(() => {
overrideModules([blessed, contrib]);
});
// describe('CLI Test Suite', () => {
// before(() => {
// overrideModules([blessed, contrib]);
// });

it('should initialize blessed, submit a form, add row and clean up form', async (context) => {
const view = new View();
// it('should initialize blessed, submit a form, add row and clean up form', async (context) => {
// const view = new View();

const screenMock = context.mock.method(blessed, "screen");
const tableMockMock = context.mock.method(contrib, "table");
// const screenMock = context.mock.method(blessed, "screen");
// const tableMockMock = context.mock.method(contrib, "table");

const onSubmit = context.mock.fn()
const onReset = context.mock.fn()
// const onSubmit = context.mock.fn()
// const onReset = context.mock.fn()

context.mock.method(blessed, 'form', (...args) => {
return {
on: onSubmit,
reset: onReset
}
})
// context.mock.method(blessed, 'form', (...args) => {
// return {
// on: onSubmit,
// reset: onReset
// }
// })

const addRow = context.mock.method(view, view.addRow.name);
const resetForm = context.mock.method(view, view.resetForm.name);
// const addRow = context.mock.method(view, view.addRow.name);
// const resetForm = context.mock.method(view, view.resetForm.name);

await Controller.init({ view: view });
// await Controller.init({
// view: view,
// service: {
// createUser: context.mock.fn(),
// getUsers: context.mock.fn(async () => []),
// }
// });

assert.strictEqual(screenMock.mock.callCount(), 1);
assert.strictEqual(tableMockMock.mock.callCount(), 1);
// assert.strictEqual(screenMock.mock.callCount(), 1);
// assert.strictEqual(tableMockMock.mock.callCount(), 1);

const onSubmitMock = onSubmit.mock
assert.strictEqual(onSubmitMock.callCount(), 1);
// const onSubmitMock = onSubmit.mock
// assert.strictEqual(onSubmitMock.callCount(), 1);

const onSubmitFn = onSubmitMock.calls[0].arguments[1]
const data = { name: 'Erick Wendel', age: 28, email: 'e@e.com' }
onSubmitFn(data)
// const onSubmitFn = onSubmitMock.calls[0].arguments[1]
// const data = { name: 'Erick Wendel', age: 28, email: 'e@e.com' }
// onSubmitFn(data)

assert.strictEqual(addRow.mock.callCount(), 1)
assert.strictEqual(resetForm.mock.callCount(), 1)
// assert.strictEqual(addRow.mock.callCount(), 1)
// assert.strictEqual(resetForm.mock.callCount(), 1)

});
});
// });
// });
10 changes: 7 additions & 3 deletions app/app/test/controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ function generateView() {
}

describe('Controller unit test', () => {
it('#init', () => {
it('#init', async () => {
const view = generateView()
const controller = Controller.init({
view
await Controller.init({
view,
service: {
createUser: mock.fn(async () => ({})),
getUsers: mock.fn(async () => []),
}
})

assert.strictEqual(view.configureFormSubmit.mock.callCount(), 1)
Expand Down
18 changes: 13 additions & 5 deletions app/app/test/web.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ describe('Web app test suite', () => {

const addRow = context.mock.method(view, view.addRow.name)

_controller = Controller.init({
view
_controller = await Controller.init({
view,
service: {
createUser: context.mock.fn(),
getUsers: context.mock.fn(async () => []),
}
})

const [
Expand Down Expand Up @@ -87,7 +91,7 @@ describe('Web app test suite', () => {

})

it('given invalid data, should call alert with message', (context) => {
it('given invalid data, should call alert with message', async (context) => {
const document = getDocument(context.mock, {
age: '',
name: '',
Expand All @@ -99,8 +103,12 @@ describe('Web app test suite', () => {
const addRow = context.mock.method(view, view.addRow.name)
const notify = context.mock.method(view, view.notify.name)

_controller = Controller.init({
view
_controller = await Controller.init({
view,
service: {
createUser: context.mock.fn(),
getUsers: context.mock.fn(async () => []),
}
})

const [
Expand Down

0 comments on commit 2c9cec0

Please sign in to comment.