Skip to content

Commit

Permalink
fix(sdk): readState returns the entire output, not just state
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Oct 4, 2023
1 parent d4a5ffd commit 025bed6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
5 changes: 0 additions & 5 deletions sdk/src/lib/readState/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { fromPromise, of } from 'hyper-async'
import { defaultTo, pipe, prop } from 'ramda'

import { loadStateSchema } from '../../dal.js'

Expand All @@ -24,9 +23,5 @@ export function readWith (env) {
.chain(fromPromise(
loadStateSchema.implement(env.loadState)
))
.map(pipe(
defaultTo({}),
prop('state')
))
}
}
30 changes: 5 additions & 25 deletions sdk/src/lib/readState/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,25 @@ import * as assert from 'node:assert'
import { readWith } from './read.js'

describe('read', () => {
test('should return the loaded state', async () => {
test('should return the output', async () => {
const read = readWith({
loadState: async (args) => {
assert.deepStrictEqual(args, {
id: 'contract-123',
sortKey: 'sort-key-123'
})

return { state: { foo: 'bar' } }
return { state: { foo: 'bar' }, messages: [{ foo: 'bar' }] }
}
})

await read({
const res = await read({
id: 'contract-123',
sortKey: 'sort-key-123'
}).toPromise()
})

test('should return undefined if no state', async () => {
const read = readWith({
loadState: async (args) => ({ no_state: { foo: 'bar' } })
})

await read({
id: 'contract-123',
sortKey: 'sort-key-123'
}).toPromise()
.then(state => assert.ok(state === undefined))
})

test('should return undefined if no value', async () => {
const read = readWith({
loadState: async (args) => undefined
assert.deepStrictEqual(res, {
state: { foo: 'bar' }, messages: [{ foo: 'bar' }]
})

await read({
id: 'contract-123',
sortKey: 'sort-key-123'
}).toPromise()
.then(state => assert.ok(state === undefined))
})
})

0 comments on commit 025bed6

Please sign in to comment.