Skip to content

Commit

Permalink
Fix the utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eaviles committed Nov 29, 2018
1 parent 2f26dff commit c591875
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
7 changes: 5 additions & 2 deletions lib/consumer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('lib/consumer', () => {

test("activate registers a consumer and return its ARN if it doesn't exists", async () => {
await expect(consumer.activate(ctx)).resolves.toMatch(/^arn:aws:kinesis/);
expect(client.registerStreamConsumer).toBeCalledWith({
expect(client.registerStreamConsumer).toHaveBeenCalledWith({
ConsumerName: 'foo',
StreamARN: 'bar'
});
Expand Down Expand Up @@ -116,7 +116,10 @@ describe('lib/consumer', () => {
callback();
});
await expect(consumer.activate(ctx)).resolves.toMatch(/^arn:aws:kinesis/);
expect(client.registerStreamConsumer).toBeCalledWith({ ConsumerName: 'foo', StreamARN: 'bar' });
expect(client.registerStreamConsumer).toHaveBeenCalledWith({
ConsumerName: 'foo',
StreamARN: 'bar'
});
expect(logger.debug.mock.calls).toEqual([
['Checking if the "foo" consumer for "baz" exists…'],
['Waiting for the stream consumer to complete deletion…'],
Expand Down
15 changes: 8 additions & 7 deletions lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const utils = require('./utils');
jest.mock('./consumer');
jest.mock('./stream');
jest.mock('./utils');
jest.mock('./shard-subscriber');

describe('lib/index', () => {
const consumerName = 'foo';
Expand All @@ -19,6 +20,7 @@ describe('lib/index', () => {
consumer.activate.mockClear();
stream.activate.mockClear();
stream.encrypt.mockClear();
stream.getShards.mockClear();
stream.tag.mockClear();
utils.noop.mockClear();
});
Expand Down Expand Up @@ -54,13 +56,13 @@ describe('lib/index', () => {
test('connect should instantiate an AWS SDK Kinesis object', async () => {
const kinesis = new Kinesis({ consumerName, streamName, foo: 'bar' });
await kinesis.connect();
expect(AwsKinesis).toBeCalledWith({ foo: 'bar' });
expect(AwsKinesis).toHaveBeenCalledWith({ foo: 'bar' });
});

test('connect should activate a stream', async () => {
const kinesis = new Kinesis({ consumerName, streamName });
await kinesis.connect();
expect(stream.activate).toBeCalledWith({
expect(stream.activate).toHaveBeenCalledWith({
client: expect.any(Object),
compression: undefined,
consumerArn: undefined,
Expand All @@ -70,7 +72,6 @@ describe('lib/index', () => {
logger: expect.any(Object),
options: {},
shardCount: 1,
shardSubscribers: [],
shards: [],
streamArn: undefined,
streamName: 'bar',
Expand All @@ -83,7 +84,7 @@ describe('lib/index', () => {
await kinesis.connect();
const [[{ logger }]] = stream.activate.mock.calls;
const { noop } = utils;
expect(logger).toEqual({ debug: noop, error: noop });
expect(logger).toEqual({ debug: noop, error: noop, warn: noop });
expect(noop.mock.calls).toEqual([
['Trying to connect the client…'],
['Creating subscribers for the stream shards using "foo"…'],
Expand All @@ -92,7 +93,7 @@ describe('lib/index', () => {
});

test('connect should use the provided logger', async () => {
const logger = { debug: jest.fn(), error: jest.fn() };
const logger = { debug: jest.fn(), error: jest.fn(), warn: jest.fn() };
await new Kinesis({ consumerName, streamName, logger }).connect();
expect(utils.noop).not.toBeCalled();
expect(logger.debug.mock.calls).toEqual([
Expand All @@ -106,14 +107,14 @@ describe('lib/index', () => {

test('connect should encrypt a stream if provided with the encryption options', async () => {
await new Kinesis({ consumerName, streamName, encryption: { foo: 'bar' } }).connect();
expect(stream.encrypt).toBeCalled();
expect(stream.encrypt).toHaveBeenCalled();
const [[{ encryption }]] = stream.encrypt.mock.calls;
expect(encryption).toEqual({ foo: 'bar' });
});

test('connect should tag a stream if provided with the tags option', async () => {
await new Kinesis({ consumerName, streamName, tags: { foo: 'bar' } }).connect();
expect(stream.tag).toBeCalled();
expect(stream.tag).toHaveBeenCalled();
const [[{ tags }]] = stream.tag.mock.calls;
expect(tags).toEqual({ foo: 'bar' });
});
Expand Down
12 changes: 11 additions & 1 deletion lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

const utils = require('./utils');

const { isJson, noop, wait } = utils;
const { isJson, noop, safeJsonParse, wait } = utils;

describe('lib/utils', () => {
test('the module exports the expected', () => {
expect(utils).toEqual({
isJson: expect.any(Function),
noop: expect.any(Function),
safeJsonParse: expect.any(Function),
wait: expect.any(Function)
});
});
Expand All @@ -26,6 +27,15 @@ describe('lib/utils', () => {
expect(() => foo()).not.toThrow();
});

test('the safeJsonParse function returns an object from a JSON string', () => {
const json = JSON.stringify({ foo: 'bar' });
expect(safeJsonParse(json)).toEqual({ foo: 'bar' });
});

test('the safeJsonParse function returns an empty object on invalid JSON strings', () => {
expect(safeJsonParse('{foo}')).toEqual({});
});

test('the wait function can be used to delay the execution of the next statement', async () => {
const before = new Date().getTime();
await wait(32);
Expand Down

0 comments on commit c591875

Please sign in to comment.