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

feat: add library mocks #159

Merged
merged 3 commits into from
Mar 9, 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
7 changes: 7 additions & 0 deletions docs/.astro/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,13 @@ declare module 'astro:content' {
collection: "docs";
data: InferEntrySchema<"docs">
} & { render(): Render[".mdx"] };
"reference/testing.mdx": {
id: "reference/testing.mdx";
slug: "reference/testing";
body: string;
collection: "docs";
data: InferEntrySchema<"docs">
} & { render(): Render[".mdx"] };
"reference/theming.mdx": {
id: "reference/theming.mdx";
slug: "reference/theming";
Expand Down
5 changes: 5 additions & 0 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ export default defineConfig({
label: 'Debugging',
link: '/reference/debugging/'
},
{
label: 'Testing',
link: '/reference/testing/',
badge: 'New'
},
{
label: 'Errors',
link: '/reference/errors/'
Expand Down
66 changes: 66 additions & 0 deletions docs/src/content/docs/reference/testing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: Testing
---

import Seo from '../../../components/Seo.astro'
import Badge from '../../../components/Badge.astro'

<Seo
seo={{
title: 'Testing',
description: 'How to test react-native-unistyles?'
}}
>

<Badge label="All platforms" />
<Badge label="2.5.0" />

Unistyles ships with own mocks that will help you to test your components that consume `useStyles` and `createStyleSheet`.

### Using mocks

In order to use the library mocks you need to either use `jest` or set `process.env.NODE_ENV` to `test`.

```json /NODE_ENV=test/
"scripts": {
"start": "react-native start",
"test": "NODE_ENV=test vitest"
},
```

### NativeEventEmitter

To mock `NativeEventEmitter` that is imported internally from `react-native` you can use the following code:

1. Create `jest.setup.js` file:

```javascript /NativeEventEmitter/
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');
```

2. If you use `jest.config.js`:

```diff lang="js"
module.exports = {
preset: 'react-native',
+ setupFiles: ['./jest.setup.js'],
};
```

3. If you use `jest` in `package.json`:

```diff lang="json"
{
"jest": {
"preset": "react-native",
+ "setupFiles": ["./jest.setup.js"]
}
}
```

### Support

Mocks support basic operations and are designed to mimic the setting of themes and breakpoints.
However, some operations, such as using `plugins` or `adaptiveThemes`, are not supported.

</Seo>
1 change: 1 addition & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const isAndroid = Platform.OS === 'android'
export const isMobile = isIOS || isAndroid
export const isServer = typeof window === 'undefined'
export const isDev = process.env.NODE_ENV !== 'production'
export const isTest = process.env.NODE_ENV === 'test' || process.env.JEST_WORKER_ID !== undefined || typeof jest !== 'undefined'

export const ScreenOrientation = {
Landscape: 'landscape',
Expand Down
11 changes: 10 additions & 1 deletion src/core/Unistyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import { UnistylesModule } from './UnistylesModule'
import { UnistylesRuntime } from './UnistylesRuntime'
import { UnistyleRegistry } from './UnistyleRegistry'
import type { UnistylesBridge } from '../types'
import { UnistylesError, isWeb } from '../common'
import { UnistylesError, isTest, isWeb } from '../common'
import { UnistylesMockedBridge, UnistylesMockedRegistry, UnistylesMockedRuntime } from './mocks'

class Unistyles {
private _runtime: UnistylesRuntime
private _registry: UnistyleRegistry
private _bridge: UnistylesBridge

constructor() {
if (isTest) {
this._bridge = new UnistylesMockedBridge() as unknown as UnistylesBridge
this._registry = new UnistylesMockedRegistry(this._bridge) as unknown as UnistyleRegistry
this._runtime = new UnistylesMockedRuntime(this._bridge, this._registry) as unknown as UnistylesRuntime

return
}

const isInstalled = UnistylesModule?.install() ?? false

if (!isInstalled) {
Expand Down
26 changes: 26 additions & 0 deletions src/core/mocks/UnistylesMockedBridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @ts-nocheck
import type { UnistylesThemes, UnistylesBreakpoints } from '../../global'

export class UnistylesMockedBridge {
constructor() {}

public screenWidth() {}
public screenHeight() {}
public enabledPlugins() {}
public hasAdaptiveThemes() {}
public themeName() {}
public breakpoint() {}
public colorScheme() {}
public contentSizeCategory() {}
public sortedBreakpointPairs() {}
public insets() {}
public statusBar() {}
public navigationBar() {}
public themes(themes: Array<keyof UnistylesThemes>) {}
public useBreakpoints(breakpoints: UnistylesBreakpoints) {}
public useTheme(name: keyof UnistylesThemes) {}
public updateTheme(name: keyof UnistylesThemes) {}
public useAdaptiveThemes(enable: boolean) {}
public addPlugin(pluginName: string, notify: boolean) {}
public removePlugin(pluginName: string) {}
}
47 changes: 47 additions & 0 deletions src/core/mocks/UnistylesMockedRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// @ts-nocheck
import type { UnistylesBreakpoints, UnistylesThemes } from '../../global'
import type { UnistylesPlugin, UnistylesConfig, UnistylesBridge } from '../../types'

export class UnistylesMockedRegistry {
public config: UnistylesConfig = {}
public breakpoints: UnistylesBreakpoints = {} as UnistylesBreakpoints
public sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]> = []
public plugins: Array<UnistylesPlugin> = []
public themes: UnistylesThemes = {} as UnistylesThemes
public themeNames: Array<keyof UnistylesThemes> = []

constructor(private unistylesBridge: UnistylesBridge) {}

public addThemes = (themes: UnistylesThemes) => {
this.themes = themes
this.themeNames = Object.keys(themes) as Array<keyof UnistylesThemes>

return this
}
public addBreakpoints = (breakpoints: UnistylesBreakpoints) => {
this.breakpoints = breakpoints
this.sortedBreakpointPairs = Object
.entries(breakpoints)
.sort((breakpoint1, breakpoint2) => {
const [, value1] = breakpoint1
const [, value2] = breakpoint2

return value1 - value2
}) as Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>

return this
}
public addConfig = (config: UnistylesConfig) => {}
public getTheme = (forName: keyof UnistylesThemes) => {
if (this.themeNames.length === 0) {
return {} as UnistylesThemes[keyof UnistylesThemes]
}

return this.themes[forName]
}

public addPlugin = (plugin: UnistylesPlugin, notify: boolean = true) => {}
public removePlugin = (plugin: UnistylesPlugin) => {}
public updateTheme = (name: keyof UnistylesThemes, theme: UnistylesThemes[keyof UnistylesThemes]) => {}
public hasTheme = (name: keyof UnistylesThemes) => true
}
92 changes: 92 additions & 0 deletions src/core/mocks/UnistylesMockedRuntime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// @ts-nocheck
import { ScreenOrientation } from '../../common'
import type { UnistylesBridge, UnistylesPlugin } from '../../types'
import type { UnistylesThemes } from '../../global'
import type { UnistylesMockedRegistry } from './UnistylesMockedRegistry'
import type { UnistyleRegistry } from '../UnistyleRegistry'

export class UnistylesMockedRuntime {
private unistylesRegistry: UnistylesMockedRegistry

constructor(private unistylesBridge: UnistylesBridge, private unistylesRegistry: UnistyleRegistry) {
this.unistylesRegistry = unistylesRegistry as unknown as UnistylesMockedRegistry
}

public get colorScheme() {
return 'dark'
}

public get hasAdaptiveThemes() {
return true
}

public get themeName() {
return this.unistylesRegistry.themeNames.length > 0
? this.unistylesRegistry.themeNames.at(0)
: undefined
}

public get contentSizeCategory() {
return 'unspecified'
}

public get breakpoint() {
if (this.unistylesRegistry.sortedBreakpointPairs.length === 0) {
return undefined
}

const firstBreakpoint = this.unistylesRegistry.sortedBreakpointPairs.at(0)

return firstBreakpoint
? firstBreakpoint.at(0)
: undefined
}

public get breakpoints() {
return this.unistylesRegistry.breakpoints
}

public get enabledPlugins() {
return this.unistylesRegistry.plugins
}

public get screen() {
return {
width: 360,
height: 800
}
}

public get insets() {
return {
top: 0,
right: 0,
bottom: 0,
left: 0
}
}

public get statusBar() {
return {
height: 24,
width: 800
}
}

public get navigationBar() {
return {
height: 0,
width: 0
}
}

public get orientation() {
return ScreenOrientation.Portrait
}

public setTheme = (name: keyof UnistylesThemes) => true
public updateTheme = (name: keyof UnistylesThemes, theme: UnistylesThemes[keyof UnistylesThemes]) => {}
public setAdaptiveThemes = (enabled: boolean) => {}
public addPlugin = (plugin: UnistylesPlugin) => {}
public removePlugin = (plugin: UnistylesPlugin) => {}
}
3 changes: 3 additions & 0 deletions src/core/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { UnistylesMockedBridge } from './UnistylesMockedBridge'
export { UnistylesMockedRegistry } from './UnistylesMockedRegistry'
export { UnistylesMockedRuntime } from './UnistylesMockedRuntime'
Loading