import { describe, expect, test } from 'vitest' import userEvent from '@testing-library/user-event' import Alpine from 'alpinejs' import { nextTick } from 'alpinejs/src/nextTick' import { xMaska } from '../src/alpine' Alpine.plugin(xMaska) Alpine.start() let input: HTMLInputElement const user = userEvent.setup() const prepareInput = async (markup: string) => { document.body.innerHTML = markup await nextTick() return document.querySelector('input') } describe('init', () => { test('with string', async () => { input = await prepareInput(``) expect(input).toHaveValue('1-2') }) test('with data attr', async () => { input = await prepareInput(``) expect(input).toHaveValue('1-2') }) test('with wrapper', async () => { input = await prepareInput( `
` ) expect(input).toHaveValue('1-2') }) test('with expression', async () => { input = await prepareInput( `` ) expect(input).toHaveValue('1-') await user.type(input, '23') expect(input).toHaveValue('1-2') }) test('bind x-model', async () => { input = await prepareInput( `
` ) const span = document.querySelector('span') const checkbox = ( document.querySelector('input[type="checkbox"]') ) expect(input).toHaveValue('1-') expect(span).toHaveTextContent('1-') await user.type(input, '23') expect(input).toHaveValue('1-2') expect(span).toHaveTextContent('1-2') await user.click(checkbox) await user.clear(input) await user.type(input, '1') expect(input).toHaveValue('1') expect(span).toHaveTextContent('1') await user.click(checkbox) expect(input).toHaveValue('1-') expect(span).toHaveTextContent('1-') }) }) describe('bindings', () => { test('bind masked', async () => { input = await prepareInput( `
` ) const span = document.querySelector('span') await user.type(input, '123') expect(input).toHaveValue('1-2') expect(span).toHaveTextContent('1-2') }) test('bind unmasked', async () => { input = await prepareInput( `
` ) const span = document.querySelector('span') await user.type(input, '123') expect(input).toHaveValue('1-2') expect(span).toHaveTextContent('12') }) test('bind completed', async () => { input = await prepareInput( `
` ) const span = document.querySelector('span') await user.type(input, '1') expect(input).toHaveValue('1') expect(span).not.toBeVisible() await user.type(input, '2') expect(input).toHaveValue('1-2') expect(span).toBeVisible() }) test('bind masked with onMask', async () => { input = await prepareInput( `
` ) const span = document.querySelector('span') await user.type(input, '123') expect(input).toHaveValue('1-2') expect(span).toHaveTextContent('1-2') }) })