Skip to content

Commit 6bf796e

Browse files
committed
fix tests: TextEncoder is not defined when importing react-router in a test
1 parent 36ac104 commit 6bf796e

File tree

4 files changed

+43
-42
lines changed

4 files changed

+43
-42
lines changed

frontend/src/components/client/client-helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import {useBlocker} from 'react-router';
44
import {ClientModel, ClientType} from './models/ClientModels';
55
import {saveClient as dispatchSaveClient} from '../../actions/index';
66
import {t} from '../utils';
7-
import {BtwResponse, formatBtw} from '../controls/form-controls/inputs/BtwInput';
7+
import {BtwResponse} from '../controls/form-controls/inputs/BtwInput';
88
import {getNewClient} from './models/getNewClient';
99
import {ConfacState} from '../../reducers/app-state';
1010
import {ConfigModel} from '../config/models/ConfigModel';
1111
import {countries} from '../controls/other/CountrySelect';
1212
import {InvoiceLine} from '../invoice/models/InvoiceLineModels';
13+
import {formatBtw} from '../controls/form-controls/inputs/BtwInputHelpers';
1314

1415

1516
/** Returns a ClientModel with the data from the BTW lookup */

frontend/src/components/controls/form-controls/inputs/BtwInput.tsx

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import {EnhanceInputWithAddons} from '../../../enhancers/EnhanceInputWithAddons'
99
import {t} from '../../../utils';
1010
import {Icon} from '../../Icon';
1111
import {buildRequest} from '../../../../actions/initialLoad';
12+
import {parseBtw, BtwInRequest, formatBtw} from './BtwInputHelpers';
1213

13-
/** Default to this country code if none provided */
14-
const DefaultBtwCountry = 'BE';
1514
/** Spinning loader starts when > than this chars have been typed */
1615
const SmallestPossibleBtwLength = 8;
1716

@@ -30,7 +29,6 @@ type BtwInputProps = BaseInputProps<string> & {
3029
onFinalize?: (btw: string, btwInfo?: BtwResponse) => void;
3130
}
3231

33-
const BtwInRequest = t('taxRequest');
3432

3533
const BtwInputComponent = ({value, onChange, onBtwChange, onFinalize, ...props}: BtwInputProps) => {
3634
const [inputValue, setInputValue] = useState(value || '');
@@ -137,40 +135,3 @@ export type BtwResponse = {
137135
async function fetchBtwInfo(btw: string): Promise<BtwResponse> {
138136
return (await fetch(buildRequest(`/clients/btw/${btw}`)).then(result => result.json())) as BtwResponse;
139137
}
140-
141-
142-
143-
144-
145-
/**
146-
* Convert input to an unformatted Btw number
147-
*/
148-
export function parseBtw(str: string): string {
149-
if (!str) {
150-
return '';
151-
}
152-
153-
if (str === BtwInRequest) {
154-
return str;
155-
}
156-
157-
let btw = str.toUpperCase().replace(/[^0-9A-Z]/g, '');
158-
if (!/^[A-Z]{2}/.test(btw)) {
159-
btw = DefaultBtwCountry + btw;
160-
}
161-
if (!/^[A-Z]{2}[A-Z0-9]{8,12}$/.test(btw)) {
162-
return btw;
163-
}
164-
165-
if (btw.startsWith('BE') && btw.length < 12) {
166-
btw = btw.slice(0, 2) + btw.slice(2).padStart(10, '0');
167-
}
168-
169-
return btw;
170-
}
171-
172-
/**
173-
* Formats to "BE 0123.456.789"
174-
* Expects input to be in "BE0123456789"
175-
*/
176-
export const formatBtw = (str: string): string => str.replace(/(\w{2})(\d{4})(\d{3})(\d{3})/, '$1 $2.$3.$4');
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {t} from '../../../utils';
2+
3+
/** Default to this country code if none provided */
4+
const DefaultBtwCountry = 'BE';
5+
6+
export const BtwInRequest = t('taxRequest');
7+
8+
/**
9+
* Convert input to an unformatted Btw number
10+
*/
11+
export function parseBtw(str: string): string {
12+
if (!str) {
13+
return '';
14+
}
15+
16+
if (str === BtwInRequest) {
17+
return str;
18+
}
19+
20+
let btw = str.toUpperCase().replace(/[^0-9A-Z]/g, '');
21+
if (!/^[A-Z]{2}/.test(btw)) {
22+
btw = DefaultBtwCountry + btw;
23+
}
24+
if (!/^[A-Z]{2}[A-Z0-9]{8,12}$/.test(btw)) {
25+
return btw;
26+
}
27+
28+
if (btw.startsWith('BE') && btw.length < 12) {
29+
btw = btw.slice(0, 2) + btw.slice(2).padStart(10, '0');
30+
}
31+
32+
return btw;
33+
}
34+
35+
/**
36+
* Formats to "BE 0123.456.789"
37+
* Expects input to be in "BE0123456789"
38+
*/
39+
export const formatBtw = (str: string): string => str.replace(/(\w{2})(\d{4})(\d{3})(\d{3})/, '$1 $2.$3.$4');

frontend/src/components/controls/form-controls/inputs/spec/BtwInput.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {t} from '../../../../utils';
2-
import {parseBtw, formatBtw} from '../BtwInput';
2+
import {parseBtw, formatBtw} from '../BtwInputHelpers';
33

44

55
// BE0123456789

0 commit comments

Comments
 (0)