Skip to content

Commit

Permalink
Add jest (#2)
Browse files Browse the repository at this point in the history
* Revert absolute imports

* Add jest
  • Loading branch information
arhtudormorar authored May 1, 2024
1 parent 9868679 commit 1414c47
Show file tree
Hide file tree
Showing 18 changed files with 253 additions and 129 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ node_modules
# builds
build
dist
out
.idea
.idea/workspace.xml
.rpt2_cache
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- [Reverted absolute imports](https://github.com/multiversx/mx-sdk-dapp-core/pull/2)
- [Added network store](https://github.com/multiversx/mx-sdk-dapp-core/pull/1)

## [[v0.0.0]](https://github.com/multiversx/mx-sdk-dapp-core)] - 2024-04-17
7 changes: 0 additions & 7 deletions __tests__/react-library.test.js

This file was deleted.

28 changes: 28 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
verbose: true,
testEnvironment: 'jsdom',
testTimeout: 10000,
moduleDirectories: ['node_modules', 'src'],
modulePaths: ['<rootDir>/src'],
roots: ['<rootDir>/src'],
transform: {
'^.+\\.(ts|js|tsx|jsx)$': ['@swc/jest']
},
transformIgnorePatterns: ['node_modules/(^.+\\\\.(ts|js|tsx|jsx)$)'],
testMatch: [
'**/__tests__/**/*.[jt]s?(x)',
'**/?(*.)+(spec|test|bgTest).[jt]s?(x)'
],
moduleFileExtensions: [
// Place tsx and ts to beginning as suggestion from Jest team
// https://jestjs.io/docs/configuration#modulefileextensions-arraystring
'tsx',
'ts',
'web.js',
'js',
'web.ts',
'web.tsx',
'json',
'node'
]
};
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "@multiversx/sdk-dapp-core",
"version": "0.0.0-alpha.0",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"main": "out/index.js",
"module": "out/index.js",
"types": "out/index.d.ts",
"description": "A library to hold core logic for building TypeScript dApps on the MultiversX blockchain",
"author": "MultiversX",
"license": "MIT",
Expand All @@ -13,17 +12,16 @@
"test": "__tests__"
},
"files": [
"dist"
"out/**/*"
],
"repository": {
"type": "git",
"url": "git+https://github.com/multiversx/mx-sdk-dapp-core.git"
},
"scripts": {
"test": "node ./__tests__/react-library.test.js",
"compile": "npm run tsc",
"compile-next": "tsc --p tsconfig.next.json",
"tsc": "tsc -p . --outDir ./dist"
"compile": "tsc",
"test": "jest",
"compile-next": "tsc --p tsconfig.next.json"
},
"publishConfig": {
"access": "public"
Expand All @@ -38,11 +36,13 @@
},
"devDependencies": {
"@multiversx/sdk-core": ">= 12.18.0",
"axios": ">=1.6.5",
"@swc/core": "^1.4.17",
"@swc/jest": "^0.2.36",
"@types/node": "18.19.0",
"@typescript-eslint/eslint-plugin": "5.0.0",
"@typescript-eslint/parser": "5.14.0",
"eslint": "8.24.0",
"axios": ">=1.6.5",
"eslint": "8.1.0",
"eslint-config-prettier": "8.5.0",
"eslint-config-standard": "17.0.0",
"eslint-import-resolver-typescript": "2.4.0",
Expand All @@ -54,6 +54,6 @@
"jest-environment-jsdom": "28.1.3",
"prettier": "3.1.0",
"ts-jest": "29.1.1",
"typescript": "5.0.4"
"typescript": "^5.4.2"
}
}
4 changes: 2 additions & 2 deletions src/apiCalls/configuration/getNetworkConfigFromApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { getCleanApiAddress } from 'store/slices/network/actions/getCleanApiAddress';
import { ApiNetworkConfigType } from 'types/network.types';
import { getCleanApiAddress } from '../../store/slices/network/actions/getCleanApiAddress';
import { ApiNetworkConfigType } from '../../types/network.types';
import { NETWORK_CONFIG_ENDPOINT } from '../endpoints';

const urlIsValid = (url: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/apiCalls/configuration/getServerConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { NetworkType } from 'types/network.types';
import { NetworkType } from '../../types/network.types';
import { CONFIG_ENDPOINT } from '../endpoints';

export async function getServerConfiguration(apiAddress: string) {
Expand Down
13 changes: 13 additions & 0 deletions src/core/Logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Logger } from './Logger';

describe('Logger', () => {
it('should call log with correct arguments', () => {
const logger = Logger;
const mockLog = jest.spyOn(logger, 'log');
const message = 'Test message';

logger.log(message);

expect(mockLog).toHaveBeenCalledWith(message);
});
});
2 changes: 1 addition & 1 deletion src/store/slices/account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createStore } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import { storage } from 'constants/storage';
import { storage } from '../../constants/storage';
import { getKeys } from './helpers/getKeys';
import { getReactStore } from './helpers/getReactStore';
import { GetSetType } from './helpers/types';
Expand Down
8 changes: 4 additions & 4 deletions src/store/slices/network/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getServerConfiguration } from 'apiCalls/configuration/getServerConfiguration';
import { fallbackNetworkConfigurations } from 'constants/network';
import { EnvironmentsEnum } from 'types/enums.types';
import { CustomNetworkType } from 'types/network.types';
import { getServerConfiguration } from '../../../apiCalls/configuration/getServerConfiguration';
import { fallbackNetworkConfigurations } from '../../../constants/network';
import { EnvironmentsEnum } from '../../../types/enums.types';
import { CustomNetworkType } from '../../../types/network.types';
import { store } from './network';

export type InitializeNetworkPropsType = {
Expand Down
8 changes: 4 additions & 4 deletions src/store/slices/network/actions/initializeNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getServerConfiguration } from 'apiCalls/configuration/getServerConfiguration';
import { fallbackNetworkConfigurations } from 'constants/network';
import { EnvironmentsEnum } from 'types/enums.types';
import { CustomNetworkType } from 'types/network.types';
import { getServerConfiguration } from '../../../../apiCalls/configuration/getServerConfiguration';
import { fallbackNetworkConfigurations } from '../../../../constants/network';
import { EnvironmentsEnum } from '../../../../types/enums.types';
import { CustomNetworkType } from '../../../../types/network.types';
import { store } from '../network';

export type InitializeNetworkPropsType = {
Expand Down
2 changes: 1 addition & 1 deletion src/store/slices/network/actions/refreshChainID.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNetworkConfigFromApi } from 'apiCalls/configuration/getNetworkConfigFromApi';
import { getNetworkConfigFromApi } from '../../../../apiCalls/configuration/getNetworkConfigFromApi';
import { store } from '../network';

export async function refreshChainID() {
Expand Down
2 changes: 1 addition & 1 deletion src/store/slices/network/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CurrentNetworkType } from 'types/network.types';
import { CurrentNetworkType } from '../../../types/network.types';

export const defaultNetwork: CurrentNetworkType = {
id: 'not-configured',
Expand Down
4 changes: 2 additions & 2 deletions src/store/slices/network/network.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { devtools, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import { createStore } from 'zustand/vanilla';
import { storage } from 'constants/storage';
import { NetworkType } from 'types/network.types';
import { storage } from '../../../constants/storage';
import { NetworkType } from '../../../types/network.types';
import { getKeys } from '../helpers/getKeys';
import { getReactStore } from '../helpers/getReactStore';
import { GetSetType } from '../helpers/types';
Expand Down
19 changes: 10 additions & 9 deletions src/store/slices/shared/listenToLogout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ export const listenToLogout: <
T extends (state: T, options?: { detail?: { message: string } }) => void
>(
w: T
) => StateCreator<T, [], [], {}> = (wrapper) => (_set, get) => {
listenToCustomEvent(
SharedActionsEnum.LOGOUT,
(event: CustomEvent<{ detail?: { message: string } }>) => {
wrapper(get(), event.detail);
}
);
return {};
};
) => StateCreator<T, [], [], Record<string, unknown>> =
(wrapper) => (_set, get) => {
listenToCustomEvent(
SharedActionsEnum.LOGOUT,
(event: CustomEvent<{ detail?: { message: string } }>) => {
wrapper(get(), event.detail);
}
);
return {};
};
1 change: 1 addition & 0 deletions src/store/slices/shared/loggerMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const loggerMiddleware =
): S =>
config(
(...args) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_state, _shouldReplace, ...rest] = args;

const [{ type }] = rest as any;
Expand Down
4 changes: 1 addition & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
"compilerOptions": {
"module": "CommonJS",
"target": "es2015",
"outDir": "dist",
"outDir": "out",
"lib": [
"es2015",
"dom"
],
"sourceMap": true,
"rootDir": "./src",
"baseUrl": "./src",
"allowJs": true,
"strict": true,
"strictPropertyInitialization": true,
Expand Down
Loading

0 comments on commit 1414c47

Please sign in to comment.