-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const { AssertionError } = require('assert') | ||
const { StreamName } = require('./stream-name') | ||
|
||
exports.createCategory = (category) => { | ||
const commandStreamName = (id, extra = {}) => { | ||
if (id === null || id === undefined) { | ||
throw new AssertionError({ | ||
message: 'ID is required to create a command stream name', | ||
actual: id, | ||
stackStartFn: commandStreamName | ||
}) | ||
} | ||
|
||
const types = extra.types || [] | ||
if (extra.type) { | ||
types.unshift(extra.type) | ||
} | ||
types.unshift('command') | ||
|
||
return StreamName.create(category, id, { types }) | ||
} | ||
|
||
return { commandStreamName } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const { AssertionError } = require('assert') | ||
const { createCategory } = require('../category') | ||
|
||
const A_CATEGORY = 'SomeCategory' | ||
const AN_ENTITY_ID = 'abc123' | ||
|
||
const catchError = (fn) => { | ||
try { | ||
fn() | ||
} catch (error) { | ||
return error | ||
} | ||
} | ||
|
||
describe('category', () => { | ||
describe('commandStreamName', () => { | ||
describe('given no types specified', () => { | ||
it('adds "command" type to stream name', () => { | ||
const category = createCategory(A_CATEGORY) | ||
|
||
const streamName = category.commandStreamName(AN_ENTITY_ID) | ||
|
||
expect(streamName).toBe('SomeCategory:command-abc123') | ||
}) | ||
}) | ||
|
||
describe('given types specified', () => { | ||
it('adds "command" and the specified types to stream name', () => { | ||
const category = createCategory(A_CATEGORY) | ||
|
||
const types = ['other', 'thang'] | ||
const streamName = category.commandStreamName(AN_ENTITY_ID, { types }) | ||
|
||
expect(streamName).toBe('SomeCategory:command+other+thang-abc123') | ||
}) | ||
}) | ||
|
||
describe('given a type is specified', () => { | ||
it('adds "command" and the specified type to stream name', () => { | ||
const category = createCategory(A_CATEGORY) | ||
|
||
const type = 'other' | ||
const streamName = category.commandStreamName(AN_ENTITY_ID, { type }) | ||
|
||
expect(streamName).toBe('SomeCategory:command+other-abc123') | ||
}) | ||
}) | ||
|
||
describe('when id is null', () => { | ||
it('throws an error', () => { | ||
const category = createCategory(A_CATEGORY) | ||
|
||
const error = catchError(() => category.commandStreamName(null)) | ||
|
||
expect(error).toBeDefined() | ||
expect(error).toBeInstanceOf(AssertionError) | ||
expect(error.message).toBe('ID is required to create a command stream name') | ||
}) | ||
}) | ||
|
||
describe('when id is undefined', () => { | ||
it('throws an error', () => { | ||
const category = createCategory(A_CATEGORY) | ||
|
||
const error = catchError(() => category.commandStreamName()) | ||
|
||
expect(error).toBeDefined() | ||
expect(error).toBeInstanceOf(AssertionError) | ||
expect(error.message).toBe('ID is required to create a command stream name') | ||
}) | ||
}) | ||
}) | ||
}) |