Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 51 additions & 25 deletions jest.mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,58 @@
import React from 'react'
import { Image } from 'react-native'

// Mock font awesome
const Icon = 'Icon'
const getIconType = (prefix) => {
switch (prefix) {
case 'fas':
return 'solid'
case 'fal':
return 'light'
case 'fab':
return 'brand'
default:
throw new Error()
// === Globals ===

if (typeof global.requestIdleCallback === 'undefined') {
global.requestIdleCallback = function (callback) {
return setTimeout(function () {
callback({
timeRemaining: function () {
return 50
},
didTimeout: false,
})
}, 0)
}
}
jest.mock('@fortawesome/react-native-fontawesome', () => ({
FontAwesomeIcon: (faIcon) => (
<Icon
testID={faIcon.testID}
color={faIcon.color}
name={faIcon.icon.iconName}
size={faIcon.size}
style={faIcon.style}
type={getIconType(faIcon.icon.prefix)}
transform={faIcon.transform}
/>
),
}))

if (typeof global.cancelIdleCallback === 'undefined') {
global.cancelIdleCallback = function (id) {
clearTimeout(id)
}
}

// === Third-party libraries ===

jest.mock('@fortawesome/react-native-fontawesome', () => {
const Icon = 'Icon'

const getIconType = (prefix) => {
switch (prefix) {
case 'fas':
return 'solid'
case 'fal':
return 'light'
case 'fab':
return 'brand'
default:
throw new Error()
}
}

return {
FontAwesomeIcon: (faIcon) => (
<Icon
testID={faIcon.testID}
color={faIcon.color}
name={faIcon.icon.iconName}
size={faIcon.size}
style={faIcon.style}
type={getIconType(faIcon.icon.prefix)}
transform={faIcon.transform}
/>
),
}
})

Image.getSizeWithHeaders = jest.fn(() => Promise.resolve({ width: 0, height: 0 }))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@observation.org/react-native-components",
"version": "1.67.0",
"version": "1.68.0",
"main": "src/index.ts",
"repository": "git@github.com:observation/react-native-components.git",
"author": "Observation.org",
Expand Down
10 changes: 10 additions & 0 deletions src/@types/requestIdleCallback.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare global {
function requestIdleCallback(
callback: (deadline: { timeRemaining(): number; didTimeout: boolean }) => void,
options?: { timeout: number },
): number

function cancelIdleCallback(handle: number): void
}

export {}
6 changes: 3 additions & 3 deletions src/hooks/useShowBlurView.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react'
import { InteractionManager, Platform } from 'react-native'
import { Platform } from 'react-native'

import { useFocusEffect } from '@react-navigation/native'

Expand All @@ -21,14 +21,14 @@ const useShowBlurView = () => {
React.useCallback(() => {
let timer: Timeout
const timeout = isIOSDevice ? 0 : 200
const task = InteractionManager.runAfterInteractions(() => {
const taskHandle = requestIdleCallback(() => {
timer = setTimeout(() => {
setShowBlurView(true)
}, timeout)
})

return () => {
task.cancel()
cancelIdleCallback(taskHandle)
clearTimeout(timer)
setShowBlurView(isIOSDevice)
}
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "@react-native/typescript-config",
"typeRoots": ["./src/@types", "./node_modules/@types"],
"compilerOptions": {
"types": ["jest"],
},
Expand Down