import { beVisible, haveLength, haveText, html, notBeVisible, test } from '../../utils'
test('renders loops with x-for',
html`
`,
({ get }) => {
get('span:nth-of-type(1)').should(haveText('foo'))
get('span:nth-of-type(2)').should(notBeVisible())
get('button').click()
get('span:nth-of-type(1)').should(haveText('foo'))
get('span:nth-of-type(2)').should(haveText('bar'))
}
)
test('renders loops with x-for that have space or newline',
html`
`,
({ get }) => {
get('#1 span').should(haveText('oof'))
get('#1 h1').should(haveText('rab'))
get('#2 span').should(haveText('ofo'))
get('#2 h1').should(haveText('arb'))
}
)
test('removes all elements when array is empty and previously had one item',
html`
`,
({ get }) => {
get('span').should(beVisible())
get('button').click()
get('span').should(notBeVisible())
}
)
test('removes all elements when array is empty and previously had multiple items',
html`
`,
({ get }) => {
get('span:nth-of-type(1)').should(beVisible())
get('span:nth-of-type(2)').should(beVisible())
get('span:nth-of-type(3)').should(beVisible())
get('button').click()
get('span:nth-of-type(1)').should(notBeVisible())
get('span:nth-of-type(2)').should(notBeVisible())
get('span:nth-of-type(3)').should(notBeVisible())
}
)
test('elements inside of loop are reactive',
html`
`,
({ get }) => {
get('span').should(beVisible())
get('h1').should(haveText('first'))
get('h2').should(haveText('bar'))
get('button').click()
get('span').should(beVisible())
get('h1').should(haveText('first'))
get('h2').should(haveText('baz'))
}
)
test('components inside of loop are reactive',
html`
`,
({ get }) => {
get('span').should(haveText('bar'))
get('button').click()
get('span').should(haveText('bob'))
}
)
test('components inside a plain element of loop are reactive',
html`
`,
({ get }) => {
get('span').should(haveText('bar'))
get('button').click()
get('span').should(haveText('bob'))
}
)
test('adding key attribute moves dom nodes properly',
html`
`,
({ get }) => {
let haveOgIndex = index => el => expect(el[0].og_loop_index).to.equal(index)
get('#assign').click()
get('span:nth-of-type(1)').should(haveOgIndex(0))
get('span:nth-of-type(2)').should(haveOgIndex(1))
get('#reorder').click()
get('span:nth-of-type(1)').should(haveOgIndex(undefined))
get('span:nth-of-type(2)').should(haveOgIndex(1))
get('span:nth-of-type(3)').should(haveOgIndex(0))
get('span:nth-of-type(4)').should(haveOgIndex(undefined))
}
)
test('can key by index',
html`
`,
({ get }) => {
let haveOgIndex = index => el => expect(el[0].og_loop_index).to.equal(index)
get('#assign').click()
get('span:nth-of-type(1)').should(haveOgIndex(0))
get('span:nth-of-type(2)').should(haveOgIndex(1))
get('#reorder').click()
get('span:nth-of-type(1)').should(haveOgIndex(0))
get('span:nth-of-type(2)').should(haveOgIndex(1))
get('span:nth-of-type(3)').should(haveOgIndex(undefined))
}
)
test('can use index inside of loop',
html`
`,
({ get }) => {
get('h1').should(haveText(0))
get('h2').should(haveText(0))
}
)
test('can use third iterator param (collection) inside of loop',
html`
`,
({ get }) => {
get('h1').should(haveText('foo'))
get('h2').should(haveText('foo'))
}
)
test('listeners in loop get fresh iteration data even though they are only registered initially',
html`
`,
({ get }) => {
get('h1:nth-of-type(1) h2:nth-of-type(1)').should(beVisible())
get('h1:nth-of-type(1) h2:nth-of-type(2)').should(beVisible())
get('h1:nth-of-type(2) h2:nth-of-type(1)').should(notBeVisible())
get('button').click()
get('h1:nth-of-type(1) h2:nth-of-type(1)').should(beVisible())
get('h1:nth-of-type(1) h2:nth-of-type(2)').should(beVisible())
get('h1:nth-of-type(2) h2:nth-of-type(1)').should(beVisible())
}
)
test('x-for updates the right elements when new item are inserted at the beginning of the list',
html`
`,
({ get }) => {
get('h1:nth-of-type(1) h2:nth-of-type(1)').should(haveText('foo: bob'))
get('h1:nth-of-type(1) h2:nth-of-type(2)').should(haveText('foo: lob'))
get('h1:nth-of-type(2) h2:nth-of-type(1)').should(haveText('baz: bab'))
get('h1:nth-of-type(2) h2:nth-of-type(2)').should(haveText('baz: lab'))
}
)
test('sibling x-for do not interact with each other',
html`
`,
({ get }) => {
get('h1:nth-of-type(1)').should(haveText('1'))
get('h2:nth-of-type(1)').should(haveText('1'))
get('h2:nth-of-type(2)').should(haveText('2'))
get('button').click()
get('h1:nth-of-type(1)').should(haveText('1'))
get('h1:nth-of-type(2)').should(haveText('2'))
get('h2:nth-of-type(1)').should(haveText('1'))
get('h2:nth-of-type(2)').should(haveText('2'))
get('h2:nth-of-type(3)').should(haveText('3'))
}
)
test('x-for over range using i in x syntax',
html`
`,
({ get }) => get('span').should(haveLength('10'))
)
test('x-for over range using i in property syntax',
html`
`,
({ get }) => get('span').should(haveLength('10'))
)
test.retry(2)('x-for with an array of numbers',
`
`,
({ get }) => {
get('span').should(haveLength('0'))
get('#first').click()
get('span').should(haveLength('1'))
get('#second').click()
get('span').should(haveLength('2'))
}
)
test('x-for works with undefined',
`