Skip to content

Commit

Permalink
message-store: add Category class
Browse files Browse the repository at this point in the history
  • Loading branch information
mpareja committed Oct 6, 2019
1 parent 5cebf85 commit 87db5f2
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
24 changes: 24 additions & 0 deletions message-store/category.js
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 }
}
1 change: 1 addition & 0 deletions message-store/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
...require('./category'),
...require('./examples'),
...require('./expected-version-error'),
...require('./stream-name')
Expand Down
73 changes: 73 additions & 0 deletions message-store/test/category.test.js
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')
})
})
})
})

0 comments on commit 87db5f2

Please sign in to comment.