mirror of
https://github.com/selfxyz/self.git
synced 2026-02-19 02:24:25 -05:00
chore: less flaky nfc test (#1738)
* attempt to make test less flaky * fix flaky test again * lint
This commit is contained in:
@@ -4,9 +4,9 @@
|
||||
|
||||
import type { FC } from 'react';
|
||||
import React, { useCallback } from 'react';
|
||||
import { Dimensions, Image, Pressable, StyleSheet } from 'react-native';
|
||||
import { Image, Pressable, StyleSheet } from 'react-native';
|
||||
import LinearGradient from 'react-native-linear-gradient';
|
||||
import { Separator, Text, XStack, YStack } from 'tamagui';
|
||||
import { Text, XStack, YStack } from 'tamagui';
|
||||
import { useNavigation } from '@react-navigation/native';
|
||||
|
||||
import type { AadhaarData } from '@selfxyz/common';
|
||||
@@ -20,12 +20,7 @@ import {
|
||||
import { WarningTriangleIcon } from '@selfxyz/euclid/dist/components/icons/WarningTriangleIcon';
|
||||
import { RoundFlag } from '@selfxyz/mobile-sdk-alpha/components';
|
||||
import {
|
||||
black,
|
||||
red600,
|
||||
slate100,
|
||||
slate300,
|
||||
slate400,
|
||||
slate500,
|
||||
white,
|
||||
yellow500,
|
||||
} from '@selfxyz/mobile-sdk-alpha/constants/colors';
|
||||
@@ -39,20 +34,14 @@ import CardBackgroundId5 from '@/assets/images/card_background_id5.png';
|
||||
import CardBackgroundId6 from '@/assets/images/card_background_id6.png';
|
||||
import DevCardLogo from '@/assets/images/dev_card_logo.svg';
|
||||
import DevCardWave from '@/assets/images/dev_card_wave.svg';
|
||||
import LogoGray from '@/assets/images/logo_gray.svg';
|
||||
import SelfLogoPending from '@/assets/images/self_logo_pending.svg';
|
||||
import WaveOverlay from '@/assets/images/wave_overlay.png';
|
||||
import { getSecurityLevel } from '@/components/homescreen/cardSecurityBadge';
|
||||
import { cardStyles } from '@/components/homescreen/cardStyles';
|
||||
import KycIdCard from '@/components/homescreen/KycIdCard';
|
||||
import { SvgXml } from '@/components/homescreen/SvgXmlWrapper';
|
||||
import { useCardDimensions } from '@/hooks/useCardDimensions';
|
||||
import { getBackgroundIndex } from '@/utils/cardBackgroundSelector';
|
||||
import {
|
||||
formatDateFromYYMMDD,
|
||||
getDocumentAttributes,
|
||||
getNameAndSurname,
|
||||
} from '@/utils/documentAttributes';
|
||||
import { getDocumentAttributes } from '@/utils/documentAttributes';
|
||||
import { registerModalCallbacks } from '@/utils/modalCallbackRegistry';
|
||||
|
||||
const CARD_BACKGROUNDS = [
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
// This pattern avoids hoisting issues with jest.mock
|
||||
import { Buffer } from 'buffer';
|
||||
|
||||
import { scan } from '@/integrations/nfc/nfcScanner';
|
||||
import { parseScanResponse, scan } from '@/integrations/nfc/nfcScanner';
|
||||
import { PassportReader } from '@/integrations/nfc/passportReader';
|
||||
|
||||
// Declare global variable for platform OS that can be modified per-test
|
||||
@@ -37,23 +37,6 @@ jest.mock('react-native', () => ({
|
||||
// Ensure the Node Buffer implementation is available to the module under test
|
||||
global.Buffer = Buffer;
|
||||
|
||||
// The static import above captures Platform.OS at load time. To test different platforms,
|
||||
// we need to clear the module cache and re-import with the current global.mockPlatformOS.
|
||||
const getFreshParseScanResponse = () => {
|
||||
jest.resetModules();
|
||||
jest.doMock('react-native', () => ({
|
||||
Platform: {
|
||||
get OS() {
|
||||
return global.mockPlatformOS;
|
||||
},
|
||||
Version: 14,
|
||||
select: (obj: Record<string, unknown>) =>
|
||||
obj[global.mockPlatformOS] || obj.default,
|
||||
},
|
||||
}));
|
||||
return require('@/integrations/nfc/nfcScanner').parseScanResponse;
|
||||
};
|
||||
|
||||
describe('parseScanResponse', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
@@ -63,7 +46,6 @@ describe('parseScanResponse', () => {
|
||||
|
||||
it('parses iOS response', () => {
|
||||
// Platform.OS is already mocked as 'ios' by default
|
||||
const parseScanResponse = getFreshParseScanResponse();
|
||||
const mrz =
|
||||
'P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<L898902C<3UTO6908061F9406236ZE184226B<<<<<14';
|
||||
const response = JSON.stringify({
|
||||
@@ -128,7 +110,6 @@ describe('parseScanResponse', () => {
|
||||
it('parses Android response', () => {
|
||||
// Set Platform.OS to android for this test
|
||||
global.mockPlatformOS = 'android';
|
||||
const parseScanResponse = getFreshParseScanResponse();
|
||||
|
||||
const mrz =
|
||||
'P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<L898902C<3UTO6908061F9406236ZE184226B<<<<<14';
|
||||
@@ -196,7 +177,6 @@ describe('parseScanResponse', () => {
|
||||
|
||||
it('handles malformed iOS response', () => {
|
||||
// Platform.OS is already mocked as 'ios' by default
|
||||
const parseScanResponse = getFreshParseScanResponse();
|
||||
const response = '{"invalid": "json"';
|
||||
|
||||
expect(() => parseScanResponse(response)).toThrow();
|
||||
@@ -205,7 +185,6 @@ describe('parseScanResponse', () => {
|
||||
it('handles malformed Android response', () => {
|
||||
// Set Platform.OS to android for this test
|
||||
global.mockPlatformOS = 'android';
|
||||
const parseScanResponse = getFreshParseScanResponse();
|
||||
|
||||
const response = {
|
||||
mrz: 'valid_mrz',
|
||||
@@ -218,7 +197,6 @@ describe('parseScanResponse', () => {
|
||||
|
||||
it('handles missing required fields', () => {
|
||||
// Platform.OS is already mocked as 'ios' by default
|
||||
const parseScanResponse = getFreshParseScanResponse();
|
||||
const response = JSON.stringify({
|
||||
// Providing minimal data but missing critical passportMRZ field
|
||||
dataGroupHashes: JSON.stringify({
|
||||
@@ -238,7 +216,6 @@ describe('parseScanResponse', () => {
|
||||
|
||||
it('handles invalid hex data in dataGroupHashes', () => {
|
||||
// Platform.OS is already mocked as 'ios' by default
|
||||
const parseScanResponse = getFreshParseScanResponse();
|
||||
const response = JSON.stringify({
|
||||
dataGroupHashes: JSON.stringify({
|
||||
DG1: { sodHash: 'invalid_hex' },
|
||||
|
||||
@@ -77,6 +77,7 @@ describe('Points API - Signature Logic', () => {
|
||||
|
||||
let mockWallet: any;
|
||||
let consoleErrorSpy: jest.SpyInstance;
|
||||
let originalBufferFrom: typeof Buffer.from;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
@@ -101,6 +102,11 @@ describe('Points API - Signature Logic', () => {
|
||||
// Mock ethers.getBytes
|
||||
(ethers.getBytes as jest.Mock).mockReturnValue(mockSignatureBytes);
|
||||
|
||||
// Save original Buffer.from before mocking (global.Buffer is shared across
|
||||
// all test files in the same worker, so we must restore it to avoid
|
||||
// poisoning other test files like nfcScanner.test.ts)
|
||||
originalBufferFrom = global.Buffer.from;
|
||||
|
||||
// Mock Buffer.from for base64 conversion
|
||||
global.Buffer.from = jest.fn().mockReturnValue({
|
||||
toString: jest.fn().mockReturnValue(mockSignatureBase64),
|
||||
@@ -113,6 +119,7 @@ describe('Points API - Signature Logic', () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
global.Buffer.from = originalBufferFrom;
|
||||
consoleErrorSpy.mockRestore();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user