Skip to content

Commit

Permalink
Merge branch 'main' into feature/docs-website
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz authored Oct 11, 2023
2 parents 412512b + bf27e3a commit a91a8fa
Show file tree
Hide file tree
Showing 16 changed files with 899 additions and 3,914 deletions.
24 changes: 0 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,3 @@ jobs:

- name: Run tests
run: yarn test

test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup
uses: ./.github/actions/setup

- name: Run unit tests
run: yarn test --maxWorkers=2 --coverage

build-library:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup
uses: ./.github/actions/setup

- name: Build package
run: yarn prepare
15 changes: 13 additions & 2 deletions example/src/examples/Breakpoints.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { Text, View } from 'react-native'
import { Image, Text, View } from 'react-native'
import { createStyleSheet, useStyles } from '../styles'

// Use breakpoints for some values
Expand All @@ -8,12 +8,16 @@ export const Breakpoints: React.FunctionComponent = () => {

return (
<View style={styles.dynamicContainer}>
<Text>
<Text style={styles.text}>
Breakpoint demo, resize me :)
</Text>
<Text>
Row or column?
</Text>
<Image
source={{ uri: 'https://picsum.photos/600/300' }}
style={styles.image}
/>
</View>
)
}
Expand All @@ -29,5 +33,12 @@ const stylesheet = createStyleSheet(theme => ({
md: 'column'
},
backgroundColor: theme.colors.sky
},
text: {
fontWeight: 'bold'
},
image: {
resizeMode: 'cover',
overlayColor: 'red'
}
}))
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-unistyles",
"version": "1.0.0-beta.5",
"version": "1.0.0-beta.6",
"description": "Level up your React Native StyleSheet",
"scripts": {
"test": "jest",
Expand Down Expand Up @@ -39,19 +39,19 @@
},
"devDependencies": {
"@commitlint/config-conventional": "17.7.0",
"@evilmartians/lefthook": "1.5.0",
"@react-native/eslint-config": "0.73.1",
"@release-it/conventional-changelog": "5.1.1",
"@release-it/conventional-changelog": "7.0.2",
"@testing-library/react-hooks": "8.0.1",
"@types/jest": "29.5.5",
"@types/react": "18.2.24",
"@types/react": "18.2.28",
"@types/react-native": "0.72.3",
"@typescript-eslint/eslint-plugin": "6.7.4",
"@typescript-eslint/eslint-plugin-tslint": "6.7.4",
"@typescript-eslint/parser": "6.7.4",
"@typescript-eslint/eslint-plugin": "6.7.5",
"@typescript-eslint/eslint-plugin-tslint": "6.7.5",
"@typescript-eslint/parser": "6.7.5",
"commitlint": "17.7.2",
"concurrently": "8.2.1",
"del-cli": "5.1.0",
"eslint": "8.50.0",
"eslint": "8.51.0",
"eslint-config-codemask": "1.1.7",
"eslint-plugin-functional": "6.0.0",
"eslint-plugin-import": "2.28.1",
Expand All @@ -67,8 +67,9 @@
"jest": "29.7.0",
"react": "18.2.0",
"react-native": "0.72.5",
"react-native-builder-bob": "0.23.0",
"react-native-builder-bob": "0.23.1",
"react-native-web": "0.19.9",
"react-test-renderer": "18.2.0",
"release-it": "16.2.1",
"typescript": "5.2.2"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBreakpointFromScreenWidth, getValueForBreakpoint, sortAndValidateBreakpoints } from './breakpoints'
import { getBreakpointFromScreenWidth, getValueForBreakpoint, sortAndValidateBreakpoints } from '../utils'
import type { ScreenSize, SortedBreakpointEntries } from '../types'

describe('breakpoints', () => {
Expand Down
158 changes: 158 additions & 0 deletions src/__tests__/createUnistyles.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import React from 'react'
import { StyleSheet } from 'react-native'
import type { ViewStyle } from 'react-native'
import { renderHook } from '@testing-library/react-hooks'
import { createUnistyles } from '../createUnistyles'
import { UnistylesTheme } from '../UnistylesTheme'
import type { CustomNamedStyles } from '../types'

jest.mock('../hooks', () => ({
useDimensions: jest.fn(() => ({
width: 500,
height: 1000
}))
}))

describe('createUnistyles', () => {
describe('createStyleSheet', () => {
it('should work exactly the same like StyleSheet.create', () => {
const breakpoints = {
xs: 0
}
const { createStyleSheet } = createUnistyles(breakpoints)

const styles = {
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
text: {
fontWeight: 'bold',
fontSize: 32
}
} as const

expect(createStyleSheet(styles)).toEqual(StyleSheet.create(styles))
})

it('should inject the theme to the createStyleSheet', () => {
const theme = {
colors: {
barbie: '#ff9ff3',
oak: '#1dd1a1',
sky: '#48dbfb',
fog: '#c8d6e5',
aloes: '#00d2d3'
}
}
const breakpoints = {
xs: 0
}
const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, typeof theme>(breakpoints)
const stylesheet = createStyleSheet(theme => ({
container: {
backgroundColor: theme.colors.barbie
}
}))
const { result } = renderHook(() => useStyles(stylesheet), {
// @ts-ignore
wrapper: ({ children }) => (
<UnistylesTheme theme={theme}>
{children}
</UnistylesTheme>
)
})

expect(result.current.theme).toEqual(theme)
expect(result.current.styles).not.toBe(Function)
expect(result.current.styles.container.backgroundColor).toEqual(theme.colors.barbie)
})
})

describe('useStyles', () => {
it('should return empty object for optional stylesheet', () => {
const breakpoints = {
xs: 0
}
const { useStyles } = createUnistyles(breakpoints)
const { result } = renderHook(() => useStyles())

expect(result.current.styles).toEqual({})
expect(result.current.theme).toEqual({})
})

it ('should choose single value from breakpoints', () => {
const breakpoints = {
xs: 0,
sm: 200,
md: 500,
lg: 1000
}

const { useStyles, createStyleSheet } = createUnistyles(breakpoints)
const stylesheet = createStyleSheet({
container: {
flex: 1,
justifyContent: 'center',
alignItems: {
xs: 'row',
md: 'column'
}
}
})
const { result } = renderHook(() => useStyles(stylesheet))

expect(result.current.styles.container.alignItems).toEqual('column')
})

it ('should choose single value from media queries', () => {
const breakpoints = {
xs: 0
}

const { useStyles, createStyleSheet } = createUnistyles(breakpoints)
const stylesheet = createStyleSheet({
container: {
flex: 1,
justifyContent: 'center',
alignItems: {
xs: 'row',
':w[300, 490]': 'column',
':w[491]': 'row'
}
}
})
const { result } = renderHook(() => useStyles(stylesheet as CustomNamedStyles<typeof stylesheet, typeof breakpoints>))

expect(result.current.styles.container.alignItems).toEqual('row')
})

it ('should choose wrap dynamic functions in Proxy', () => {
const breakpoints = {
xs: 0
}

const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, {}>(breakpoints)
const stylesheet = createStyleSheet({
container: (flex: number) => ({
flex,
justifyContent: 'center',
alignItems: {
xs: 'row',
':w[300, 490]': 'column',
':w[491]': 'row'
}
})
})
const { result } = renderHook(() => useStyles(stylesheet as CustomNamedStyles<typeof stylesheet, typeof breakpoints>))

expect(result.current.styles.container).toBeInstanceOf(Function)
expect((result.current.styles.container as (flex: number) => ViewStyle)(2)).toEqual({
flex: 2,
justifyContent: 'center',
alignItems: 'row'
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
isWithinTheHeight,
isWithinTheWidth,
isWithinTheWidthAndHeight
} from './mediaQueries'
} from '../utils'
import type { ScreenSize } from '../types'

describe('utils', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/styles.spec.ts → src/__tests__/styles.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseStyle, proxifyFunction } from './styles'
import { parseStyle, proxifyFunction } from '../utils'
import type { CustomNamedStyles, ScreenSize } from '../types'
import type { SortedBreakpointEntries } from '../types'

Expand Down
10 changes: 0 additions & 10 deletions src/createUnistyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ export const createUnistyles = <B extends Breakpoints, T = {}>(breakpoints: B) =
.entries(sortedBreakpoints) as SortedBreakpointEntries<B>

return {
/**
* @deprecated The method should not be used, proposed version by the community is createStyleSheet, will be removed in RC
*/
createStyles: <S extends CustomNamedStyles<S, B>, X>(styles: S | CustomNamedStyles<S, B> | X | ((theme: T) => X | CustomNamedStyles<X, B>)): S | X => {
if (typeof styles === 'function') {
return styles as X
}

return styles as S
},
createStyleSheet: <S extends CustomNamedStyles<S, B>, X>(styles: S | CustomNamedStyles<S, B> | X | ((theme: T) => X | CustomNamedStyles<X, B>)): S | X => {
if (typeof styles === 'function') {
return styles as X
Expand Down
Loading

0 comments on commit a91a8fa

Please sign in to comment.