Skip to content

Commit cf98de1

Browse files
committed
pr feedback
1 parent bb7f269 commit cf98de1

File tree

8 files changed

+42
-22
lines changed

8 files changed

+42
-22
lines changed

packages/mobile-sdk-alpha/src/browser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@ export type { SdkErrorCategory } from './errors';
4040

4141
export { SCANNER_ERROR_CODES, notImplemented, sdkError } from './errors';
4242
export { SelfClientContext, SelfClientProvider, useSelfClient } from './context';
43+
// Browser-only high-level component (DOM-based)
44+
export { SelfMobileSdk as SelfMobileSdkHighLevel } from './components/SelfMobileSdk';
45+
4346
export { createSelfClient } from './client';
47+
4448
export { defaultConfig } from './config/defaults';
49+
4550
export { extractMRZInfo, formatDateToYYMMDD, scanMRZ } from './mrz';
4651

4752
// Core functions

packages/mobile-sdk-alpha/src/context.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext, type PropsWithChildren, useContext, useEffect, useState } from 'react';
1+
import { createContext, type PropsWithChildren, useContext, useMemo } from 'react';
22

33
import { createSelfClient } from './client';
44
import type { Adapters, Config, SelfClient } from './types/public';
@@ -13,18 +13,7 @@ export interface SelfClientProviderProps {
1313
export { SelfClientContext };
1414

1515
export function SelfClientProvider({ config, adapters = {}, children }: PropsWithChildren<SelfClientProviderProps>) {
16-
const [client, setClient] = useState<SelfClient | null>(null);
17-
const [isLoading, setIsLoading] = useState(true);
18-
19-
useEffect(() => {
20-
const clientInstance = createSelfClient({ config, adapters });
21-
setClient(clientInstance);
22-
setIsLoading(false);
23-
}, [config, adapters]);
24-
25-
if (isLoading) {
26-
return <div>Loading...</div>; // Simple loading state, can be customized
27-
}
16+
const client = useMemo(() => createSelfClient({ config, adapters }), [config, adapters]);
2817

2918
return <SelfClientContext.Provider value={client}>{children}</SelfClientContext.Provider>;
3019
}

packages/mobile-sdk-alpha/src/hooks/useDocumentManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useLayoutEffect, useState } from 'react';
1+
import { useCallback, useEffect, useState } from 'react';
22

33
import type { DocumentData, ExternalAdapter } from '../types/ui';
44

@@ -8,7 +8,7 @@ export const useDocumentManager = (external: ExternalAdapter) => {
88
}>({});
99
const [isLoading, setIsLoading] = useState(true);
1010

11-
useLayoutEffect(() => {
11+
useEffect(() => {
1212
external
1313
.getAllDocuments()
1414
.then(documents => {

packages/mobile-sdk-alpha/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ export { SelfClientContext, SelfClientProvider, useSelfClient } from './context'
7272
// Components
7373
export { SelfMobileSdk } from './entry';
7474

75-
export { SelfMobileSdk as SelfMobileSdkHighLevel } from './components/SelfMobileSdk';
76-
7775
export { createSelfClient } from './client';
7876

7977
export { defaultConfig } from './config/defaults';
8078

79+
/** @deprecated Use createSelfClient().extractMRZInfo or import from './mrz' */
80+
export { extractMRZInfo } from './mrz';
81+
8182
export { formatDateToYYMMDD, scanMRZ } from './mrz';
8283

8384
// Core functions

packages/mobile-sdk-alpha/tests/SelfMobileSdk.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { describe, expect, it } from 'vitest';
22

33
describe('High-Level SelfMobileSdk Component', () => {
44
it('can be imported successfully', async () => {
5-
const { SelfMobileSdk } = await import('../src/components/SelfMobileSdk');
5+
const { SelfMobileSdk } = await import('../src');
66
expect(SelfMobileSdk).toBeDefined();
77
expect(typeof SelfMobileSdk).toBe('function');
88
});
99

1010
it('accepts the expected props interface', async () => {
11-
const { SelfMobileSdk } = await import('../src/components/SelfMobileSdk');
11+
const { SelfMobileSdk } = await import('../src');
1212

1313
// Test that the component accepts the expected props structure
1414
const mockExternal = {

packages/mobile-sdk-alpha/tests/client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it, vi } from 'vitest';
22

3-
import type { CryptoAdapter, NetworkAdapter, ScannerAdapter } from '../src/adapters/index';
3+
import type { CryptoAdapter, NetworkAdapter, ScannerAdapter } from '../src';
44
import { createSelfClient } from '../src/index';
55

66
describe('createSelfClient', () => {

packages/mobile-sdk-alpha/tests/client.test.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { describe, expect, it } from 'vitest';
22

33
import { createSelfClient } from '../src/index';
4-
import { expectedMRZResult, mockAdapters, sampleMRZ } from './utils/testHelpers';
4+
import { MrzParseError } from '../src/processing/mrz';
5+
import { badCheckDigitsMRZ, expectedMRZResult, invalidMRZ, mockAdapters, sampleMRZ } from './utils/testHelpers';
56

67
describe('createSelfClient API', () => {
78
it('creates a client instance with expected methods', () => {
@@ -29,4 +30,15 @@ describe('createSelfClient API', () => {
2930
expect(clientWithAllAdapters).toBeDefined();
3031
expect(typeof clientWithAllAdapters.extractMRZInfo).toBe('function');
3132
});
33+
34+
it('throws MrzParseError for malformed MRZ input', () => {
35+
const client = createSelfClient({ config: {}, adapters: mockAdapters });
36+
expect(() => client.extractMRZInfo(invalidMRZ)).toThrowError(MrzParseError);
37+
});
38+
39+
it('flags invalid check digits', () => {
40+
const client = createSelfClient({ config: {}, adapters: mockAdapters });
41+
const info = client.extractMRZInfo(badCheckDigitsMRZ);
42+
expect(info.validation.overall).toBe(false);
43+
});
3244
});

packages/mobile-sdk-alpha/tests/utils/testHelpers.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@ import type { CryptoAdapter, NetworkAdapter, ScannerAdapter } from '../../src';
33

44
// Shared test data
55
export const sampleMRZ = `P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<\nL898902C36UTO7408122F1204159ZE184226B<<<<<10`;
6+
// Intentionally malformed MRZ (invalid structure/check digits) for negative tests
7+
export const invalidMRZ = 'NOT_A_VALID_MRZ';
8+
export const badCheckDigitsMRZ = sampleMRZ.slice(0, -1) + '1';
69

710
// Shared mock adapters
811
export const mockScanner: ScannerAdapter = {
912
scan: async () => ({ mode: 'mrz', passportNumber: '', dateOfBirth: '', dateOfExpiry: '' }),
1013
};
1114

1215
export const mockNetwork: NetworkAdapter = {
13-
http: { fetch: async () => ({ ok: true }) as any },
16+
// Return a minimal stub to avoid relying on global Response in JSDOM/Node
17+
http: {
18+
fetch: async () =>
19+
({
20+
ok: true,
21+
status: 200,
22+
text: async () => '',
23+
json: async () => ({}),
24+
arrayBuffer: async () => new ArrayBuffer(0),
25+
}) as any,
26+
},
1427
ws: {
1528
connect: () => ({
1629
send: () => {},

0 commit comments

Comments
 (0)