import { haveText, html, test } from '../utils'
test('can register and use a global store',
[html`
`,
`
Alpine.store('test', { foo: 'bar' })
`],
({ get }) => {
get('span').should(haveText('bar'))
get('button').click()
get('span').should(haveText('baz'))
}
)
test('store init function is called',
[html`
`,
`
Alpine.store('test', { foo: 'bar', init() { this.foo = 'baz'} })
`],
({ get }) => {
get('span').should(haveText('baz'))
}
)
test('can use primitives as store',
[html`
`,
`
Alpine.store('test', 'bar')
`],
({ get }) => {
get('span').should(haveText('bar'))
get('button').click()
get('span').should(haveText('baz'))
}
)
test('can use number as store',
[html`
`,
`
Alpine.store('test', 0)
`],
({ get }) => {
get('span').should(haveText('0'))
get('button').click()
get('span').should(haveText('1'))
}
)
test('store\'s "this" context is reactive for init function',
[html`
`,
`
Alpine.store('test', {
init() {
document.querySelector('#button').addEventListener('click', () => {
this.count++
})
},
count: 0,
})
`],
({ get }) => {
get('span').should(haveText('0'))
get('button').click()
get('span').should(haveText('1'))
}
)