forked from epicmaxco/vuestic-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(masks): correctly handle months (feb) (epicmaxco#4347)
- Loading branch information
Showing
9 changed files
with
399 additions
and
76 deletions.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
packages/ui/src/composables/useInputMask/cursor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { type MaskToken } from './mask' | ||
import { describe, it, expect } from 'vitest' | ||
|
||
import { Cursor, CursorPosition } from './cursor' | ||
|
||
const makeTokens = (str: string) => str.split('').map((char) => ({ static: char === 's' })) as MaskToken[] | ||
|
||
const printCursor = (cursor: Cursor<MaskToken>) => { | ||
const str = (cursor as any).tokens.map((t: MaskToken) => t.static ? 's' : 'd') as string[] | ||
const i = cursor.position | ||
str[i] = '|' + (str[i] ?? '') | ||
return str.join('') | ||
} | ||
|
||
describe('useInputMask/Cursor', () => { | ||
describe('Before Char', () => { | ||
describe('move forward', () => { | ||
it('should move over one static token', () => { | ||
const cursor = new Cursor(0, makeTokens('dsd')) | ||
|
||
expect(printCursor(cursor)).toBe('|dsd') | ||
cursor.moveForward(1, CursorPosition.BeforeChar) | ||
expect(printCursor(cursor)).toBe('ds|d') | ||
}) | ||
|
||
it('should move over multiple static token', () => { | ||
const cursor = new Cursor(0, makeTokens('dsssd')) | ||
|
||
expect(printCursor(cursor)).toBe('|dsssd') | ||
cursor.moveForward(1, CursorPosition.BeforeChar) | ||
expect(printCursor(cursor)).toBe('dsss|d') | ||
}) | ||
}) | ||
|
||
describe('move back', () => { | ||
it('should move over one static token', () => { | ||
const cursor = new Cursor(3, makeTokens('dsd')) | ||
|
||
expect(printCursor(cursor)).toBe('dsd|') | ||
cursor.moveBack(1, CursorPosition.BeforeChar) | ||
expect(printCursor(cursor)).toBe('ds|d') | ||
cursor.moveBack(1, CursorPosition.BeforeChar) | ||
expect(printCursor(cursor)).toBe('|dsd') | ||
}) | ||
|
||
it('should move over multiple static token', () => { | ||
const cursor = new Cursor(5, makeTokens('dsssd')) | ||
|
||
cursor.moveBack(1, CursorPosition.BeforeChar) | ||
expect(printCursor(cursor)).toBe('dsss|d') | ||
|
||
cursor.moveBack(1, CursorPosition.BeforeChar) | ||
expect(printCursor(cursor)).toBe('|dsssd') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('After Char', () => { | ||
describe('move forward', () => { | ||
it('should move over one static token', () => { | ||
const cursor = new Cursor(0, makeTokens('dsd')) | ||
|
||
cursor.moveForward(1, CursorPosition.AfterChar) | ||
expect(printCursor(cursor)).toBe('d|sd') | ||
|
||
cursor.moveForward(1, CursorPosition.AfterChar) | ||
expect(printCursor(cursor)).toBe('dsd|') | ||
}) | ||
|
||
it('should move over multiple static token', () => { | ||
const cursor = new Cursor(0, makeTokens('dsssd')) | ||
|
||
cursor.moveForward(1, CursorPosition.AfterChar) | ||
expect(printCursor(cursor)).toBe('d|sssd') | ||
|
||
cursor.moveForward(1, CursorPosition.AfterChar) | ||
expect(printCursor(cursor)).toBe('dsssd|') | ||
}) | ||
}) | ||
describe('move back', () => { | ||
it('should move over one static token', () => { | ||
const cursor = new Cursor(3, makeTokens('dsd')) | ||
|
||
cursor.moveBack(1, CursorPosition.AfterChar) | ||
expect(printCursor(cursor)).toBe('d|sd') | ||
|
||
cursor.moveBack(1, CursorPosition.AfterChar) | ||
expect(printCursor(cursor)).toBe('|dsd') | ||
}) | ||
|
||
it('should move over multiple static token', () => { | ||
const cursor = new Cursor(5, makeTokens('dsssd')) | ||
|
||
cursor.moveBack(1, CursorPosition.AfterChar) | ||
expect(printCursor(cursor)).toBe('d|sssd') | ||
|
||
cursor.moveBack(1, CursorPosition.AfterChar) | ||
expect(printCursor(cursor)).toBe('|dsssd') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('Any Char p/move forward', () => { | ||
describe('move forward', () => { | ||
it('should move over one static token', () => { | ||
const cursor = new Cursor(0, makeTokens('dsd')) | ||
|
||
cursor.moveForward(1, CursorPosition.Any) | ||
expect(printCursor(cursor)).toBe('d|sd') | ||
|
||
cursor.moveForward(1, CursorPosition.Any) | ||
expect(printCursor(cursor)).toBe('ds|d') | ||
}) | ||
|
||
it('should move over multiple static token', () => { | ||
const cursor = new Cursor(0, makeTokens('dsssd')) | ||
|
||
cursor.moveForward(1, CursorPosition.Any) | ||
expect(printCursor(cursor)).toBe('d|sssd') | ||
|
||
cursor.moveForward(1, CursorPosition.Any) | ||
expect(printCursor(cursor)).toBe('dsss|d') | ||
|
||
cursor.moveForward(1, CursorPosition.Any) | ||
expect(printCursor(cursor)).toBe('dsssd|') | ||
}) | ||
}) | ||
|
||
describe('move back', () => { | ||
it('should move over one static token', () => { | ||
const cursor = new Cursor(3, makeTokens('dsd')) | ||
|
||
cursor.moveBack(1, CursorPosition.Any) | ||
expect(printCursor(cursor)).toBe('ds|d') | ||
|
||
cursor.moveBack(1, CursorPosition.Any) | ||
expect(printCursor(cursor)).toBe('d|sd') | ||
|
||
cursor.moveBack(1, CursorPosition.Any) | ||
expect(printCursor(cursor)).toBe('|dsd') | ||
}) | ||
|
||
it('should move over multiple static token', () => { | ||
const cursor = new Cursor(5, makeTokens('dsssd')) | ||
|
||
cursor.moveBack(1, CursorPosition.Any) | ||
expect(printCursor(cursor)).toBe('dsss|d') | ||
|
||
cursor.moveBack(1, CursorPosition.Any) | ||
expect(printCursor(cursor)).toBe('d|sssd') | ||
|
||
cursor.moveBack(1, CursorPosition.Any) | ||
expect(printCursor(cursor)).toBe('|dsssd') | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.