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

create shared mocks library #564

Merged
merged 1 commit into from
Apr 1, 2024
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ The Sia web libraries provide developers with convenient TypeScript SDKs for usi

## Internal

### Apps

- [website](apps/website) - The main [sia.tech](https://sia.tech) website with information on the Sia project and the Sia Foundation.
- [assets](apps/assets) - Powers [api.sia.tech](https://api.sia.tech) and all downloadable assets on [sia.tech](https://sia.tech) such as the Sia software releases.
- [crons](apps/crons) - Background tasks for [api.sia.tech](https://api.sia.tech) and [sia.tech](https://sia.tech).

### Testing

- [walletd-e2e](walletd-e2e) - App for testing walletd.
- [@siafoundation/mock-walletd](mock-walletd) - `walletd` data and API mock library for testing.
- [@siafoundation/mock-sia-central](mock-sia-central) - Sia Central data and API mock library for testing.

## Development

1. The Sia Web codebase is managed with the [Nx](https://nx.dev) build system. Either install `nx` globally via `npm install -g nx` or use `npx` to invoke commands.
Expand Down
12 changes: 12 additions & 0 deletions libs/mock-sia-central/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@nx/react/babel",
{
"runtime": "automatic",
"useBuiltIns": "usage"
}
]
],
"plugins": []
}
21 changes: 21 additions & 0 deletions libs/mock-sia-central/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": ["plugin:@nx/react", "../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"rules": {
"@nx/dependency-checks": [
"error",
{
"ignoredFiles": ["libs/mock-sia-central/rollup.config.js"]
}
]
},
"overrides": [
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
3 changes: 3 additions & 0 deletions libs/mock-sia-central/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mock-sia-central

Sia Central data and API mock library for testing.
17 changes: 17 additions & 0 deletions libs/mock-sia-central/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */
export default {
displayName: 'mock-sia-central',
preset: '../../jest.preset.js',
transform: {
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
'^.+\\.[tj]sx?$': [
'babel-jest',
{
presets: ['@nx/next/babel'],
plugins: ['@babel/plugin-transform-private-methods'],
},
],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/mock-sia-central',
}
11 changes: 11 additions & 0 deletions libs/mock-sia-central/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@siafoundation/mock-sia-central",
"description": "Sia Central data and API mock library for testing.",
"version": "0.0.0",
"license": "MIT",
"dependencies": {
"@siafoundation/sia-central": "0.3.3",
"playwright": "^1.42.1"
},
"types": "./src/index.d.ts"
}
42 changes: 42 additions & 0 deletions libs/mock-sia-central/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "mock-sia-central",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/mock-sia-central/src",
"projectType": "library",
"tags": [],
"targets": {
"build": {
"executor": "@nx/rollup:rollup",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/mock-sia-central",
"tsConfig": "libs/mock-sia-central/tsconfig.lib.json",
"project": "libs/mock-sia-central/package.json",
"entryFile": "libs/mock-sia-central/src/index.ts",
"external": ["react/jsx-runtime"],
"compiler": "tsc",
"outputFileName": "index.js",
"rollupConfig": "libs/mock-sia-central/rollup.config.js",
"assets": [
{
"glob": "libs/mock-sia-central/*.md",
"input": ".",
"output": "."
}
]
},
"configurations": {}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"]
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/libs/mock-sia-central"],
"options": {
"jestConfig": "libs/mock-sia-central/jest.config.ts"
}
}
}
}
18 changes: 18 additions & 0 deletions libs/mock-sia-central/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const preserveDirectives = require('rollup-plugin-preserve-directives')

// https://github.com/rollup/rollup/issues/4699#issuecomment-1465302665
function getRollupOptions(options) {
return {
...options,
output: {
...options.output,
preserveModules: true,
format: 'esm',
sourcemap: true,
},
plugins: options.plugins.concat(preserveDirectives.default()),
}
}

module.exports = getRollupOptions
1 change: 1 addition & 0 deletions libs/mock-sia-central/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './siaCentralExchangeRates'
39 changes: 39 additions & 0 deletions libs/mock-sia-central/src/siaCentralExchangeRates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { SiaCentralExchangeRatesResponse } from '@siafoundation/sia-central'
import { Page } from 'playwright'

export function getMockSiaCentralExchangeRatesResponse(): SiaCentralExchangeRatesResponse {
return {
message: 'successfully retrieved exchange rate',
type: 'success',
rates: {
sc: {
aud: '0.016136871549',
bch: '0.000021499880703',
btc: '0.000000149047',
cad: '0.014328484298',
cny: '0.076310722577',
eth: '0.0000029068532077',
eur: '0.009737538604',
gbp: '0.008359151948',
jpy: '1.600530478116',
ltc: '0.000116295710314',
rub: '0.978819669836',
scp: '0.0623627615062762',
sf: '0.000000745235',
usd: '0.010571307522',
},
},
timestamp: '2024-03-26T13:12:22.7348119Z',
}
}

export async function mockApiSiaCentralExchangeRates({ page }: { page: Page }) {
const json = getMockSiaCentralExchangeRatesResponse()
await page.route(
'https://api.siacentral.com/v2/market/exchange-rate?currencies=sc',
async (route) => {
await route.fulfill({ json })
}
)
return json
}
25 changes: 25 additions & 0 deletions libs/mock-sia-central/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"jsx": "react-jsx",
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
22 changes: 22 additions & 0 deletions libs/mock-sia-central/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": [
"node",
"@nx/react/typings/cssmodule.d.ts",
"@nx/react/typings/image.d.ts"
]
},
"exclude": [
"**/*.spec.ts",
"**/*.test.ts",
"**/*.spec.tsx",
"**/*.test.tsx",
"**/*.spec.js",
"**/*.test.js",
"**/*.spec.jsx",
"**/*.test.jsx"
],
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"]
}
19 changes: 19 additions & 0 deletions libs/mock-sia-central/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.test.tsx",
"src/**/*.spec.tsx",
"src/**/*.test.js",
"src/**/*.spec.js",
"src/**/*.test.jsx",
"src/**/*.spec.jsx",
"src/**/*.d.ts"
]
}
12 changes: 12 additions & 0 deletions libs/mock-walletd/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@nx/react/babel",
{
"runtime": "automatic",
"useBuiltIns": "usage"
}
]
],
"plugins": []
}
21 changes: 21 additions & 0 deletions libs/mock-walletd/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": ["plugin:@nx/react", "../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"rules": {
"@nx/dependency-checks": [
"error",
{
"ignoredFiles": ["libs/mock-walletd/rollup.config.js"]
}
]
},
"overrides": [
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
3 changes: 3 additions & 0 deletions libs/mock-walletd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mock-walletd

`walletd` data and API mock library for testing.
17 changes: 17 additions & 0 deletions libs/mock-walletd/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */
export default {
displayName: 'mock-walletd',
preset: '../../jest.preset.js',
transform: {
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
'^.+\\.[tj]sx?$': [
'babel-jest',
{
presets: ['@nx/next/babel'],
plugins: ['@babel/plugin-transform-private-methods'],
},
],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/mock-walletd',
}
14 changes: 14 additions & 0 deletions libs/mock-walletd/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@siafoundation/mock-walletd",
"description": "walletd data and API mock library for testing.",
"version": "0.0.0",
"license": "MIT",
"dependencies": {
"@siafoundation/types": "0.1.3",
"@siafoundation/react-walletd": "3.0.0",
"@siafoundation/units": "3.0.0",
"playwright": "^1.42.1",
"@siafoundation/mock-sia-central": "0.0.0"
},
"types": "./src/index.d.ts"
}
42 changes: 42 additions & 0 deletions libs/mock-walletd/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "mock-walletd",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/mock-walletd/src",
"projectType": "library",
"tags": [],
"targets": {
"build": {
"executor": "@nx/rollup:rollup",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/mock-walletd",
"tsConfig": "libs/mock-walletd/tsconfig.lib.json",
"project": "libs/mock-walletd/package.json",
"entryFile": "libs/mock-walletd/src/index.ts",
"external": ["react/jsx-runtime"],
"compiler": "tsc",
"outputFileName": "index.js",
"rollupConfig": "libs/mock-walletd/rollup.config.js",
"assets": [
{
"glob": "libs/mock-walletd/*.md",
"input": ".",
"output": "."
}
]
},
"configurations": {}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"]
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/libs/mock-walletd"],
"options": {
"jestConfig": "libs/mock-walletd/jest.config.ts"
}
}
}
}
18 changes: 18 additions & 0 deletions libs/mock-walletd/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const preserveDirectives = require('rollup-plugin-preserve-directives')

// https://github.com/rollup/rollup/issues/4699#issuecomment-1465302665
function getRollupOptions(options) {
return {
...options,
output: {
...options.output,
preserveModules: true,
format: 'esm',
sourcemap: true,
},
plugins: options.plugins.concat(preserveDirectives.default()),
}
}

module.exports = getRollupOptions
Loading
Loading