22import React from "react"
33import { describe , it , expect , vi } from "vitest"
44import { render , screen , fireEvent } from "@testing-library/react"
5- import {
6- FormattedTextField ,
7- integerFormatter ,
8- currencyFormatter ,
9- unlimitedIntegerFormatter ,
10- } from "../FormattedTextField"
5+ import { FormattedTextField , unlimitedIntegerFormatter } from "../FormattedTextField"
116
127// Mock VSCodeTextField to render as regular HTML input for testing
138vi . mock ( "@vscode/webview-ui-toolkit/react" , ( ) => ( {
@@ -23,56 +18,6 @@ vi.mock("@vscode/webview-ui-toolkit/react", () => ({
2318} ) )
2419
2520describe ( "FormattedTextField" , ( ) => {
26- describe ( "integerFormatter" , ( ) => {
27- it ( "should parse valid integers" , ( ) => {
28- expect ( integerFormatter . parse ( "123" ) ) . toBe ( 123 )
29- expect ( integerFormatter . parse ( "1" ) ) . toBe ( 1 )
30- } )
31-
32- it ( "should return undefined for invalid inputs" , ( ) => {
33- expect ( integerFormatter . parse ( "" ) ) . toBeUndefined ( )
34- expect ( integerFormatter . parse ( "0" ) ) . toBeUndefined ( )
35- expect ( integerFormatter . parse ( "-5" ) ) . toBeUndefined ( )
36- expect ( integerFormatter . parse ( "abc" ) ) . toBeUndefined ( )
37- } )
38-
39- it ( "should format numbers correctly" , ( ) => {
40- expect ( integerFormatter . format ( 123 ) ) . toBe ( "123" )
41- expect ( integerFormatter . format ( undefined ) ) . toBe ( "" )
42- } )
43-
44- it ( "should filter non-numeric characters" , ( ) => {
45- expect ( integerFormatter . filter ?.( "123abc" ) ) . toBe ( "123" )
46- expect ( integerFormatter . filter ?.( "a1b2c3" ) ) . toBe ( "123" )
47- } )
48- } )
49-
50- describe ( "currencyFormatter" , ( ) => {
51- it ( "should parse valid currency values" , ( ) => {
52- expect ( currencyFormatter . parse ( "123.45" ) ) . toBe ( 123.45 )
53- expect ( currencyFormatter . parse ( "$123.45" ) ) . toBe ( 123.45 )
54- expect ( currencyFormatter . parse ( "1,234.56" ) ) . toBe ( 1234.56 )
55- expect ( currencyFormatter . parse ( "0" ) ) . toBe ( 0 )
56- } )
57-
58- it ( "should return undefined for invalid inputs" , ( ) => {
59- expect ( currencyFormatter . parse ( "" ) ) . toBeUndefined ( )
60- expect ( currencyFormatter . parse ( "abc" ) ) . toBeUndefined ( )
61- expect ( currencyFormatter . parse ( "-5" ) ) . toBeUndefined ( )
62- } )
63-
64- it ( "should format currency correctly" , ( ) => {
65- expect ( currencyFormatter . format ( 123.45 ) ) . toBe ( "123.45" )
66- expect ( currencyFormatter . format ( 123 ) ) . toBe ( "123.00" )
67- expect ( currencyFormatter . format ( undefined ) ) . toBe ( "" )
68- } )
69-
70- it ( "should filter invalid currency characters" , ( ) => {
71- expect ( currencyFormatter . filter ?.( "$123.45abc" ) ) . toBe ( "$123.45" )
72- expect ( currencyFormatter . filter ?.( "1,234.56xyz" ) ) . toBe ( "1,234.56" )
73- } )
74- } )
75-
7621 describe ( "unlimitedIntegerFormatter" , ( ) => {
7722 it ( "should parse valid integers" , ( ) => {
7823 expect ( unlimitedIntegerFormatter . parse ( "123" ) ) . toBe ( 123 )
@@ -95,6 +40,11 @@ describe("FormattedTextField", () => {
9540 expect ( unlimitedIntegerFormatter . format ( undefined ) ) . toBe ( "" )
9641 expect ( unlimitedIntegerFormatter . format ( Infinity ) ) . toBe ( "" )
9742 } )
43+
44+ it ( "should filter non-numeric characters" , ( ) => {
45+ expect ( unlimitedIntegerFormatter . filter ?.( "123abc" ) ) . toBe ( "123" )
46+ expect ( unlimitedIntegerFormatter . filter ?.( "a1b2c3" ) ) . toBe ( "123" )
47+ } )
9848 } )
9949
10050 describe ( "FormattedTextField component" , ( ) => {
@@ -104,7 +54,7 @@ describe("FormattedTextField", () => {
10454 < FormattedTextField
10555 value = { 123 }
10656 onValueChange = { mockOnChange }
107- formatter = { integerFormatter }
57+ formatter = { unlimitedIntegerFormatter }
10858 data-testid = "test-input"
10959 /> ,
11060 )
@@ -119,7 +69,7 @@ describe("FormattedTextField", () => {
11969 < FormattedTextField
12070 value = { 123 }
12171 onValueChange = { mockOnChange }
122- formatter = { integerFormatter }
72+ formatter = { unlimitedIntegerFormatter }
12373 data-testid = "test-input"
12474 /> ,
12575 )
@@ -136,7 +86,7 @@ describe("FormattedTextField", () => {
13686 < FormattedTextField
13787 value = { undefined }
13888 onValueChange = { mockOnChange }
139- formatter = { integerFormatter }
89+ formatter = { unlimitedIntegerFormatter }
14090 data-testid = "test-input"
14191 /> ,
14292 )
@@ -155,7 +105,7 @@ describe("FormattedTextField", () => {
155105 < FormattedTextField
156106 value = { undefined }
157107 onValueChange = { mockOnChange }
158- formatter = { integerFormatter }
108+ formatter = { unlimitedIntegerFormatter }
159109 data-testid = "test-input"
160110 /> ,
161111 )
0 commit comments