Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCheater committed Jan 3, 2018
1 parent 4aadb00 commit 83fab4d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
20 changes: 16 additions & 4 deletions common/iterate.js
Expand Up @@ -13,19 +13,31 @@ export async function iterate(commandExecutorMap, iterator, context) {
}
command = step.value;

if (command[symbol] === CommandType.EXIT) {
if (command && command[symbol] === CommandType.EXIT) {
while (matches.length > 0) {
matches.pop();
}
resolve();
break;
return;
}

const commandExecutor = commandExecutorMap[command[symbol]];
const commandExecutor = command && commandExecutorMap[command[symbol]];
if (commandExecutor) {
result = await commandExecutor(command, context);
try {
result = await commandExecutor(command, context);
} catch (error) {
while (matches.length > 0) {
matches.pop();
}
reject(error);
return;
}
} else {
while (matches.length > 0) {
matches.pop();
}
reject(new Error('Unknown command'));
return;
}
}
}
30 changes: 30 additions & 0 deletions tests/functional/throw-error.test.js
@@ -0,0 +1,30 @@
import { Client, Bus } from 'reventex/server';

const channel = 'throw-error';

it('throw-error', async () => {
function* throwError({ subscribe, match, call, exit }) {
yield subscribe(channel);

yield match({ channel }, function*(event, { exit }) {
yield exit();
});

yield call(() => {
throw new Error();
});

yield exit();
}

const bus = new Bus();
const client = new Client(throwError, bus);

try {
await client.run();
} catch (error) {
return;
}

throw new Error();
});
30 changes: 30 additions & 0 deletions tests/functional/unknown-command.test.js
@@ -0,0 +1,30 @@
import { Client, Bus } from 'reventex/server';

const channel = 'unknown-command';

function unknown() {}

it('unknown-command', async () => {
function* unknownCommand({ subscribe, match, call, exit }) {
yield subscribe(channel);

yield match({ channel }, function*(event, { exit }) {
yield exit();
});

yield unknown();

yield exit();
}

const bus = new Bus();
const client = new Client(unknownCommand, bus);

try {
await client.run();
} catch (error) {
return;
}

throw new Error();
});

0 comments on commit 83fab4d

Please sign in to comment.