Skip to content

Commit

Permalink
added util tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Einstein committed Jun 23, 2017
1 parent 0933445 commit 7b507e3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

A handful of simple, auto-positioning tooltips

## example
## Example

```js
const MyComponent = () => (
Expand All @@ -22,6 +22,11 @@ const MyComponent = () => (
)
```

## Automatic positioning
## Automatic Positioning

Wrap any element with a Tooltip and it will automatically position itself to either the top, right, bottom, or left depending on where in the viewport it is rendered.
Wrap any element with a Tooltip and it will automatically position itself to either the top, right, bottom, or left based on where in the viewport its child element is rendered.

## Types

- `info`: (*default*) A standard tooltip with a small caret.
- `dialog`: A sleek, caret-less tooltip with a white background & slight box shadow.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-autotip",
"version": "0.0.4",
"version": "0.0.5",
"description": "A variety of simple auto-positioning tooltips",
"main": "index.js",
"module": "lib/index.js",
Expand Down
15 changes: 12 additions & 3 deletions src/TooltipContainer/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ describe('Tooltip Container', () => {

test('recieves and registers data from custom event', () => {
// keep track of events being added
const map = {}
window.addEventListener = jest.fn((event, cb) => map[event] = cb)
const events = {}
// stub event being added
window.addEventListener = jest.fn((event, cb) => events[event] = cb)
// stub remove event listener
window.removeEventListener = jest.fn()

// render component
const wrapper = mount(<TooltipContainer />)
Expand All @@ -29,7 +32,7 @@ describe('Tooltip Container', () => {
}

// call method attached to custom event
map[enums.ON_TOOLTIP]({detail})
events[enums.ON_TOOLTIP]({detail})

// take snapshot of state
const state = wrapper.state()
Expand All @@ -38,5 +41,11 @@ describe('Tooltip Container', () => {
Reflect.deleteProperty(state, 'tooltipDims')

expect(state).toEqual(detail)

// no unmount the component
wrapper.unmount()

// make sure we remove event listener on unmount
expect(window.removeEventListener.mock.calls.length).toBe(1)
})
})
16 changes: 16 additions & 0 deletions src/utils/is-empty-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// local imports
import isEmpty from './is-empty'

describe('utils', () => {
describe('isEmpty()', () => {
test('empty obj', () => {
const res = isEmpty({})
expect(res).toEqual(true)
})

test('non-empty obj', () => {
const res = isEmpty({a: 2})
expect(res).toEqual(false)
})
})
})
3 changes: 2 additions & 1 deletion src/utils/is-equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ export default function(a, b) {
return true
}

// if lengths are different
// if lens are different
if (aKeys.length !== bKeys.length) {
return false
}

// check each key/val
for (const key of aKeys) {
if (a[key] !== b[key]) {
return false
Expand Down
10 changes: 10 additions & 0 deletions src/utils/is-equal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ describe('utils', () => {
expect(res).toEqual(true)
})

test('one empty and one non-empty obj', () => {
const res = isEqual({}, {a: 2})
expect(res).toEqual(false)
})

test('two objects with diff num of keys', () => {
const res = isEqual({a: 2, b: 1}, {a: 2})
expect(res).toEqual(false)
Expand All @@ -18,5 +23,10 @@ describe('utils', () => {
const res = isEqual({a: 2, b: 1}, {b: 1, a: 2})
expect(res).toEqual(true)
})

test('two objects with same keys but diff props', () => {
const res = isEqual({a: 10, b: 11}, {a: 12, b: 13})
expect(res).toEqual(false)
})
})
})

0 comments on commit 7b507e3

Please sign in to comment.