import { beEqualTo, beVisible, haveText, html, notBeVisible, test } from '../../utils'
test('can persist number',
[html`
`],
({ get }, reload) => {
get('span').should(haveText('1'))
get('button').click()
get('span').should(haveText('2'))
reload()
get('span').should(haveText('2'))
},
)
test('can persist string',
[html`
`],
({ get }, reload) => {
get('span').should(haveText('foo'))
get('input').clear().type('bar')
get('span').should(haveText('bar'))
reload()
get('span').should(haveText('bar'))
},
)
test('can persist array',
[html`
`],
({ get }, reload) => {
get('span').should(haveText('foo-bar'))
get('button').click()
get('span').should(haveText('foo-bar-baz'))
reload()
get('span').should(haveText('foo-bar-baz'))
},
)
test('can persist object',
[html`
`],
({ get }, reload) => {
get('span').should(haveText('bar'))
get('button#one').click()
get('span').should(haveText('baz'))
reload()
get('span').should(haveText('baz'))
get('button#two').click()
get('span').should(haveText('bob'))
reload()
get('span').should(haveText('bob'))
},
)
test('can persist boolean',
[html`
Foo
`],
({ get }, reload) => {
get('span').should(notBeVisible())
get('button').click()
get('span').should(beVisible())
reload()
get('span').should(beVisible())
},
)
test('can persist multiple components using the same property',
[html`
`],
({ get }, reload) => {
get('span#one').should(haveText('foo'))
get('span#two').should(haveText('foo'))
get('button').click()
get('span#one').should(haveText('bar'))
reload()
get('span#one').should(haveText('bar'))
get('span#two').should(haveText('bar'))
},
)
test('can persist using an alias',
[html`
Foo
Foo
`],
({ get }, reload) => {
get('span#one').should(notBeVisible())
get('span#two').should(notBeVisible())
get('button').click()
get('span#one').should(notBeVisible())
get('span#two').should(beVisible())
reload()
get('span#one').should(notBeVisible())
get('span#two').should(beVisible())
},
)
test('aliases do not affect other $persist calls',
[html`
Foo
Foo
`],
({ get }, reload) => {
get('span#one').should(notBeVisible())
get('span#two').should(notBeVisible())
get('button').click()
get('span#one').should(notBeVisible())
get('span#two').should(beVisible())
reload()
get('span#one').should(notBeVisible())
get('span#two').should(beVisible())
},
)
test('can persist to custom storage',
[html`
`],
({ get, window }, reload) => {
get('span').should(haveText('foo'))
get('input').clear().type('bar')
get('span').should(haveText('bar'))
reload()
get('span').should(haveText('bar'))
window().its('sessionStorage._x_message').should(beEqualTo(JSON.stringify('bar')))
window().then((win) => {
win.sessionStorage.clear()
});
},
)
test('can persist to custom storage using an alias',
[html`
`],
({ get, window }, reload) => {
get('span').should(haveText('foo'))
get('input').clear().type('bar')
get('span').should(haveText('bar'))
window().its('sessionStorage.mymessage').should(beEqualTo(JSON.stringify('bar')))
window().then((win) => {
win.sessionStorage.clear()
});
},
)
test('can persist using global Alpine.$persist within Alpine.store',
[html`
`, `
Alpine.store('name', {
firstName: Alpine.$persist('Daniel').as('dev-name')
})
`],
({ get, window }, reload) => {
get('span').should(haveText('Daniel'))
get('input').clear().type('Malcolm')
get('span').should(haveText('Malcolm'))
reload()
get('span').should(haveText('Malcolm'))
},
)
test('multiple aliases work when using global Alpine.$persist',
[html`
`, `
Alpine.store('name', {
firstName: Alpine.$persist('John').as('first-name'),
lastName: Alpine.$persist('Doe').as('name-name')
})
`],
({ get, window }, reload) => {
get('span').should(haveText('John'))
get('p').should(haveText('Doe'))
get('input').clear().type('Joe')
get('span').should(haveText('Joe'))
get('p').should(haveText('Doe'))
reload()
get('span').should(haveText('Joe'))
get('p').should(haveText('Doe'))
},
)