Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: configure linting, hooks, jest, run lint #919

Merged
merged 4 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ node_modules
lib
yarn.lock
local.properties
.jest

# aries-bifold should be cloned separately
aries-bifold/
Expand Down
5 changes: 5 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# echo "Heloo"
npx commitlint --edit $1
5 changes: 5 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run pre-commit-lint
npm run typecheck
4 changes: 4 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run test
2 changes: 2 additions & 0 deletions app/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ __tests__
__mocks__
ios
android
metro.config.js
bifold/

# generated by bob
lib/
Expand Down
1 change: 1 addition & 0 deletions app/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
plugins: ['@typescript-eslint', 'import'],
rules: {
'no-console': 'error',
'@typescript-eslint/no-unused-vars': 'error',
// Because of early development, we only warn on ts-ignore. In future we want to move to error
'@typescript-eslint/ban-ts-comment': 'warn',
// Aries protocol defines attributes with snake case.
Expand Down
3 changes: 3 additions & 0 deletions app/.lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"**/*.{js,jsx,ts,tsx}": ["npm run lint -- --fix", "npx prettier --write"]
}
40 changes: 40 additions & 0 deletions app/__mocks__/@aries-framework/react-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
CredentialExchangeRecord,
GetFormatDataReturn,
IndyCredentialFormat,
ProofExchangeRecord,
} from '@aries-framework/core'

const useCredentials = jest.fn().mockReturnValue({ credentials: [] } as any)
const useCredentialByState = jest.fn().mockReturnValue([] as CredentialExchangeRecord[])
const useProofByState = jest.fn().mockReturnValue([] as ProofExchangeRecord[])
const mockCredentialModule = {
acceptOffer: jest.fn(),
declineOffer: jest.fn(),
getFormatData: jest.fn().mockReturnValue(Promise.resolve({} as GetFormatDataReturn<[IndyCredentialFormat]>)),
}
const mockProofModule = {
getRequestedCredentialsForProofRequest: jest.fn(),
acceptRequest: jest.fn(),
declineRequest: jest.fn(),
}
const useAgent = () => ({
agent: {
credentials: mockCredentialModule,
proofs: mockProofModule,
},
})
const useCredentialById = jest.fn()
const useProofById = jest.fn()
const useConnectionById = jest.fn()

export {
useAgent,
useConnectionById,
useCredentials,
useCredentialById,
useCredentialByState,
useProofById,
useProofByState,
}
1 change: 1 addition & 0 deletions app/__mocks__/@aries-framework/react-native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
7 changes: 7 additions & 0 deletions app/__mocks__/@react-native-async-storage/async-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const AsyncStorage = {}

AsyncStorage.getItem = () => {
return null
}

export default AsyncStorage
21 changes: 21 additions & 0 deletions app/__mocks__/custom/@react-navigation/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const navigate = jest.fn()

const navigation = {
navigate,
setOptions: jest.fn(),
getParent: jest.fn(() => ({
navigate,
})),
getState: jest.fn(() => ({
index: jest.fn(),
})),
goBack: jest.fn(),
pop: jest.fn(),
reset: jest.fn(),
}

const useNavigation = () => {
return navigation
}

export { useNavigation }
3 changes: 3 additions & 0 deletions app/__mocks__/custom/@react-navigation/native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const useFocusEffect = jest.fn()

export { useFocusEffect }
35 changes: 35 additions & 0 deletions app/__mocks__/custom/react-native-localize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const getLocales = () => [
{ countryCode: 'US', languageTag: 'en-US', languageCode: 'en', isRTL: false },
{ countryCode: 'NL', languageTag: 'nl-NL', languageCode: 'nl', isRTL: false },
{ countryCode: 'FR', languageTag: 'fr-FR', languageCode: 'fr', isRTL: false },
]

const getNumberFormatSettings = () => ({
decimalSeparator: '.',
groupingSeparator: ',',
})

const getCalendar = () => 'gregorian' // or "japanese", "buddhist"
const getCountry = () => 'US' // the country code you want
const getCurrencies = () => ['USD'] // can be empty array
const getTemperatureUnit = () => 'celsius' // or "fahrenheit"
const getTimeZone = () => 'America/Los_Angeles' // the timezone you want
const uses24HourClock = () => true
const usesMetricSystem = () => true

const addEventListener = jest.fn()
const removeEventListener = jest.fn()

export {
getLocales,
getNumberFormatSettings,
getCalendar,
getCountry,
getCurrencies,
getTemperatureUnit,
getTimeZone,
uses24HourClock,
usesMetricSystem,
addEventListener,
removeEventListener,
}
1 change: 1 addition & 0 deletions app/__mocks__/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default ''
9 changes: 9 additions & 0 deletions app/__mocks__/react-i18next.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const mockT = jest.fn((key: string) => key)
const useTranslation = jest.fn().mockReturnValue({
t: mockT,
i18n: {
language: 'en',
},
})

export { useTranslation }
4 changes: 4 additions & 0 deletions app/__mocks__/react-native-argon2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// eslint-disable-next-line
const argon2 = jest.fn().mockReturnValue({ rawHash: Promise.resolve('1234567890') })

export default argon2
6 changes: 6 additions & 0 deletions app/__mocks__/react-native-keychain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const ACCESS_CONTROL = jest.fn()
const ACCESSIBLE = jest.fn()
const SECURITY_LEVEL = jest.fn()
const STORAGE_TYPE = jest.fn()

export { ACCESS_CONTROL, ACCESSIBLE, SECURITY_LEVEL, STORAGE_TYPE }
48 changes: 48 additions & 0 deletions app/__mocks__/react-native-tcp-socket/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { JsonTransformer } from '@aries-framework/core'
import events from 'events'

// const eventEmitter = new events.EventEmitter()

let aNode = undefined

const ports = {
connect: 8001,
error: 8002,
timeout: 8003,
}

const on = (event, cb) => {
switch (event) {
case 'timeout':
if (aNode.port === ports.timeout) {
cb()
}
break
case 'error':
if (aNode.port === ports.error) {
cb()
}
break
default:
break
}
}

const client = {
on,
destroy: jest.fn(),
setTimeout: jest.fn(),
removeAllListeners: jest.fn(),
}

const createConnection = jest.fn((node, cb) => {
aNode = node

if (node.port === ports.connect) {
cb()
}

return client
})

export default { createConnection }
1 change: 1 addition & 0 deletions app/__mocks__/style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default ''
14 changes: 14 additions & 0 deletions app/__tests__/components/LoadingIcon.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { render } from '@testing-library/react-native'
import React from 'react'

import LoadingIcon from '../../src/components/LoadingIcon'

describe('LoadingIcon Component', () => {
test('renders correctly', () => {
const size = 50 // arbitrary
const color = '#333' // arbitrary
const active = true // arbitrary
const tree = render(<LoadingIcon size={size} color={color} active={active} />)
expect(tree).toMatchSnapshot()
})
})
40 changes: 40 additions & 0 deletions app/__tests__/components/__snapshots__/LoadingIcon.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`LoadingIcon Component renders correctly 1`] = `
<View
collapsable={false}
nativeID="animatedComponent"
style={
Object {
"transform": Array [
Object {
"rotate": "0deg",
},
],
}
}
>
<Text
allowFontScaling={false}
style={
Array [
Object {
"color": undefined,
"fontSize": 50,
},
Object {
"color": "#333",
},
Object {
"fontFamily": "Material Icons",
"fontStyle": "normal",
"fontWeight": "normal",
},
Object {},
]
}
>
</Text>
</View>
`;
4 changes: 3 additions & 1 deletion app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import '@formatjs/intl-datetimeformat/add-all-tz' // Add ALL tz data
import { NavigationContainer } from '@react-navigation/native'
import React from 'react'
import { AppRegistry } from 'react-native'

import App from './App'
import { name as appName } from './app.json'
import bcwallet from './src/'

const { theme } = bcwallet

import { name as appName } from './app.json'
const { ColorPallet } = theme

const navigationTheme = {
Expand Down
25 changes: 25 additions & 0 deletions app/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
preset: 'react-native',
testTimeout: 10000,
setupFiles: ['<rootDir>/jestSetup.js'],
setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleNameMapper: {
'\\.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/__mocks__/file.js',
'\\.(css|less)$': '<rootDir>/__mocks__/style.js',
},
transform: {
'^.+\\.(js|jsx|ts|tsx)$': 'babel-jest',
},
transformIgnorePatterns: ['/node_modules/(?!.*react-native.*)'],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
testPathIgnorePatterns: [
'\\.snap$',
'<rootDir>/node_modules/',
'<rootDir>/lib',
'<rootDir>/__tests__/contexts/',
'<rootDir>/__tests__/helpers/',
],
cacheDirectory: '.jest/cache',
}
7 changes: 7 additions & 0 deletions app/jestSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable no-undef */
import 'react-native-gesture-handler/jestSetup'
import mockRNLocalize from 'react-native-localize/mock'

jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper')
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter')
jest.mock('react-native-localize', () => mockRNLocalize)
Loading