Skip to content

Commit

Permalink
feat: add option to view the context state
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Feb 28, 2020
1 parent d4f680c commit 227fb0a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ export class Context extends Macroable implements ContextContract {
return frameWithVal ? frameWithVal[key] : undefined
}

/**
* Returns a merged copy of the current state. The objects are merged
* in the same order as they are resolved.
*/
private getCurrentState () {
return Object.assign(
{},
this.sharedState,
this.presenter.state,
...this._frames,
)
}

/**
* Creates a new frame scope. Think of a scope as a Javacript block
* scope, where variables defined inside the scope are only available
Expand Down Expand Up @@ -118,7 +131,11 @@ export class Context extends Macroable implements ContextContract {
* ```
*/
public resolve (key: string): any {
let value
if (key === '$state') {
return this.getCurrentState()
}

let value: any

/**
* Pull from one of the nested frames
Expand Down
75 changes: 75 additions & 0 deletions test/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,79 @@ test.group('Context', (group) => {
const fn = () => context.setOnFrame('username', 'nikk')
assert.throw(fn, 'Make sure to call {newFrame} before calling {setOnFrame}')
})

test('return template state', (assert) => {
const sharedState = {}
const data = {
username: 'virk',
}

const presenter = new Presenter(data)
const context = new Context(presenter, sharedState)

assert.deepEqual(context.resolve('$state'), {
username: 'virk',
})
})

test('return template state when frames are presenter', (assert) => {
const sharedState = {}
const data = {
username: 'virk',
}

const presenter = new Presenter(data)
const context = new Context(presenter, sharedState)

context.newFrame()
context.setOnFrame('username', 'foo')
assert.deepEqual(context.resolve('$state'), {
username: 'foo',
})
})

test('return template state when shared state is presenter', (assert) => {
const sharedState = {
username: 'nikk',
age: 22,
}

const data = {
username: 'virk',
}

const presenter = new Presenter(data)
const context = new Context(presenter, sharedState)

assert.deepEqual(context.resolve('$state'), {
username: 'virk',
age: 22,
})
})

test('return template state nested frames are present', (assert) => {
const sharedState = {}
const data = {
username: 'virk',
}

const presenter = new Presenter(data)
const context = new Context(presenter, sharedState)

context.newFrame()
context.setOnFrame('user', { username: 'virk' })

context.newFrame()
context.setOnFrame('post', { title: 'Adonis 101' })

assert.deepEqual(context.resolve('$state'), {
username: 'virk',
user: {
username: 'virk',
},
post: {
title: 'Adonis 101',
},
})
})
})

0 comments on commit 227fb0a

Please sign in to comment.