Skip to content

Commit f8af8cf

Browse files
authored
Merge pull request #11 from trurl-master/tsdx
Migrate to tsdx tooling and TypeScript
2 parents 3a05d4c + d9c7250 commit f8af8cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+15431
-18751
lines changed

.eslintrc.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

.github/workflows/main.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
on: [push]
3+
jobs:
4+
build:
5+
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}
6+
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
node: ['10.x', '12.x', '14.x']
11+
os: [ubuntu-latest, windows-latest, macOS-latest]
12+
13+
steps:
14+
- name: Checkout repo
15+
uses: actions/checkout@v2
16+
17+
- name: Use Node ${{ matrix.node }}
18+
uses: actions/setup-node@v1
19+
with:
20+
node-version: ${{ matrix.node }}
21+
22+
- name: Install deps and build (with cache)
23+
uses: bahmutov/npm-install@v1
24+
25+
- name: Lint
26+
run: yarn lint
27+
28+
- name: Test
29+
run: yarn test --ci --coverage --maxWorkers=2
30+
31+
- name: Build
32+
run: yarn build

.github/workflows/size.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: size
2+
on: [pull_request]
3+
jobs:
4+
size:
5+
runs-on: ubuntu-latest
6+
env:
7+
CI_JOB_NUMBER: 1
8+
steps:
9+
- uses: actions/checkout@v1
10+
- uses: andresz1/size-limit-action@v1
11+
with:
12+
github_token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
*.log
12
.DS_Store
23
node_modules
3-
dist
44
.cache
5-
coverage
6-
.eslintcache
5+
dist

.npmignore

Lines changed: 0 additions & 5 deletions
This file was deleted.

.prettierrc

Lines changed: 0 additions & 6 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Ivan Galiatin
3+
Copyright (c) 2021 Ivan Galiatin
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# jsdom-testing-mocks
2+
23
A set of tools for emulating browser behavior in jsdom environment
34

45
## Installation
@@ -7,39 +8,58 @@ A set of tools for emulating browser behavior in jsdom environment
78
npm install --save-dev jsdom-testing-mocks
89
```
910

11+
or
12+
13+
```sh
14+
yarn add -D jsdom-testing-mocks
15+
```
16+
1017
## Mock viewport
18+
1119
Mocks `matchMedia`, allows testing of component's behavior depending on the viewport description (supports all of the [Media Features](http://www.w3.org/TR/css3-mediaqueries/#media1)). `mockViewport` must be called before rendering the component
1220

1321
Example, using `React Testing Library`:
22+
1423
```jsx
15-
import { mockViewport } from 'jsdom-testing-mocks"
24+
import { mockViewport } from 'jsdom-testing-mocks';
1625

1726
it('shows the right lines on desktop and mobile', () => {
18-
const viewport = mockViewport({ width: '320px', height: '568px' })
27+
const viewport = mockViewport({ width: '320px', height: '568px' });
1928

20-
const { getByText, queryByText } = render(<TestComponent />)
29+
render(<TestComponent />);
2130

22-
expect(getByText('Content visible only on the phone')).toBeInTheDocument()
23-
expect(queryByText('Content visible only on desktop')).not.toBeInTheDocument()
31+
expect(
32+
screen.getByText('Content visible only on small screens')
33+
).toBeInTheDocument();
34+
35+
expect(
36+
screen.queryByText('Content visible only on small screens')
37+
).not.toBeInTheDocument();
2438

2539
act(() => {
26-
viewport.set({ width: '1440px', height: '900px' })
27-
})
40+
viewport.set({ width: '1440px', height: '900px' });
41+
});
2842

29-
expect(queryByText('Content visible only on the phone')).not.toBeInTheDocument()
30-
expect(getByText('Content visible only on desktop')).toBeInTheDocument()
43+
expect(
44+
screen.queryByText('Content visible only on small screens')
45+
).not.toBeInTheDocument();
3146

32-
viewport.cleanup()
33-
})
47+
expect(
48+
screen.getByText('Content visible only on small screens')
49+
).toBeInTheDocument();
50+
51+
viewport.cleanup();
52+
});
3453
```
3554

3655
Also, you can mock the viewport for a group of tests, using `mockViewportForTestGroup`:
56+
3757
```jsx
38-
import { mockViewportForTestGroup } from 'jsdom-testing-mocks"
58+
import { mockViewportForTestGroup } from 'jsdom-testing-mocks'
3959

4060
describe('Desktop specific tests', () => {
4161
mockViewportForTestGroup({ width: '1440px', height: '900px' })
42-
62+
4363
test('this', () = {
4464
// ...
4565
})
@@ -51,39 +71,42 @@ describe('Desktop specific tests', () => {
5171
```
5272

5373
## Mock intersection observer
74+
5475
Provides a way of triggering intersection observer events
5576

5677
Example, using `React Testing Library`:
5778

5879
```jsx
59-
import { mockIntersectionObserver } from 'jsdom-testing-mocks"
80+
import { mockIntersectionObserver } from 'jsdom-testing-mocks';
6081

61-
const intersectionObserver = mockIntersectionObserver()
82+
const intersectionObserver = mockIntersectionObserver();
6283

6384
it('loads the image when the component is in the viewport', () => {
64-
const { getByAltText, queryByAltText, container } = render(<TestComponent />)
85+
const { container } = render(<TestComponent />);
6586

66-
expect(queryByAltText('alt text')).not.toBeInTheDocument()
87+
expect(screen.queryByAltText('alt text')).not.toBeInTheDocument();
6788

6889
// when the component's root node is in the viewport - show the image
6990
act(() => {
70-
intersectionObserver.enterNode(container.firstChild)
71-
})
91+
intersectionObserver.enterNode(container.firstChild);
92+
});
7293

73-
expect(getByAltText('alt text')).toBeInTheDocument()
74-
})
94+
expect(screen.getByAltText('alt text')).toBeInTheDocument();
95+
});
7596
```
7697

7798
### API
7899

79100
`mockIntersectionObserver` returns an object, that has several useful methods:
80101

81102
#### .enterNode(node, desc) and .leaveNode(node, desc)
103+
82104
Triggers the intersection observer callback with only one node
83105
and `isIntersected` set to `true` (for `enterNode`) or `false` (for `leaveNode`).
84106
Other `IntersectionObserverEntry` params can be passed as `desc` argument
85107

86108
#### .enterAll(desc) and .leaveAll(desc)
109+
87110
Triggers the intersection observer callback for all of the observed nodes
88111
and `isIntersected` set to `true` (for `enterAll`) or `false` (for `leaveAll`).
89112
Other `IntersectionObserverEntry` params can be passed as `desc` argument

babel.config.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

example/.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.cache
3+
dist

0 commit comments

Comments
 (0)