diff --git a/entity-store/entity-store-substitute.js b/entity-store/entity-store-substitute.js index c6c555d..a0341fe 100644 --- a/entity-store/entity-store-substitute.js +++ b/entity-store/entity-store-substitute.js @@ -11,7 +11,7 @@ exports.createEntityStoreSubstitute = ({ entity: EntityClass }) => { } const fetchRecord = async (id) => { - const record = records[id] || { entity: new EntityClass(), metadata: {} } + const record = records[id] || { entity: new EntityClass(), metadata: { version: -1 } } return [record.entity, record.metadata] } diff --git a/entity-store/entity-store.js b/entity-store/entity-store.js index c373889..3dde9e2 100644 --- a/entity-store/entity-store.js +++ b/entity-store/entity-store.js @@ -20,7 +20,7 @@ exports.createEntityStore = (options) => { const fetchRecord = async (id) => { const entity = new EntityClass() const streamName = StreamName.create(category, id) - const recordMetadata = {} + const recordMetadata = { version: -1 } for await (const messageData of messageStore.read(streamName)) { const { handler, messageClass } = registry.get(messageData.type) diff --git a/entity-store/test/entity-store-substitute.test.js b/entity-store/test/entity-store-substitute.test.js index 5842d64..35c8bd0 100644 --- a/entity-store/test/entity-store-substitute.test.js +++ b/entity-store/test/entity-store-substitute.test.js @@ -65,11 +65,11 @@ describe('entity-store-substitute', () => { expect(entity.someMessagesApplied).toBe(false) }) - it('returns no other record metadata', async () => { + it('record version is -1, stream not initialized', async () => { const [entity, record] = await setup().entityStore.fetchRecord('unknown-id') expect(entity).toBeDefined() - expect(record.version).toBeUndefined() + expect(record.version).toBe(-1) }) }) }) diff --git a/entity-store/test/fetch-test-suite.js b/entity-store/test/fetch-test-suite.js index 35e752d..24446cf 100644 --- a/entity-store/test/fetch-test-suite.js +++ b/entity-store/test/fetch-test-suite.js @@ -125,16 +125,38 @@ module.exports.generateEntityStoreSuite = ({ }) describe('fetchRecord', () => { - it('returns entity with version', async () => { - const message = exampleMessage(MessageClassA) - const streamName = exampleStreamName(A_CATEGORY) - const id = StreamName.getId(streamName) - const position = await write(message, streamName) + describe('stream exists in store', () => { + it('returns entity with version', async () => { + const message = exampleMessage(MessageClassA) + const streamName = exampleStreamName(A_CATEGORY) + const id = StreamName.getId(streamName) + const position = await write(message, streamName) + + const [entity, { version }] = await entityStore.fetchRecord(id) + + expect(entity.applied).toHaveLength(1) + expect(version).toEqual(position) + }) + }) - const [entity, { version }] = await entityStore.fetchRecord(id) + describe('stream does not exist in store', () => { + it('returns entity with no messages applied', async () => { + const streamName = exampleStreamName(A_CATEGORY) + const id = StreamName.getId(streamName) - expect(entity.applied).toHaveLength(1) - expect(version).toEqual(position) + const [entity] = await entityStore.fetchRecord(id) + + expect(entity.applied).toHaveLength(0) + }) + + it('record version is -1, stream not initialized', async () => { + const streamName = exampleStreamName(A_CATEGORY) + const id = StreamName.getId(streamName) + + const [_, { version }] = await entityStore.fetchRecord(id) // eslint-disable-line no-unused-vars + + expect(version).toBe(-1) + }) }) })