/
index.js
148 lines (138 loc) · 4.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* globals describe it before after */
import assert from 'assert'
import withJet from '../src'
import React from 'react'
import {mount} from 'enzyme'
import {single} from 'redux-jet'
import {connect} from 'react-redux'
import {combineReducers} from 'redux'
import configureStore from './_configureStore'
import jet from 'node-jet'
const Component = ({admin, foo}) => <h1>{admin}{foo}</h1>
const Container = connect(({admin, foo}) => ({admin, foo}))(Component)
describe('withJet', () => {
let daemon
let peer
const users = {
john: {
password: '12345',
auth: {
fetchGroups: ['users']
}
},
admin: {
password: '333',
auth: {
fetchGroups: ['users', 'admin']
}
}
}
before(() => {
daemon = new jet.Daemon({users})
daemon.listen({wsPort: 1999})
peer = new jet.Peer({url: 'ws://localhost:1999'})
const s1 = new jet.State('foo/bar', 123, {fetchGroups: ['users']})
const s2 = new jet.State('admin/stuff', 'secret', {fetchGroups: ['admin']})
return Promise.all([
peer.connect(),
peer.add(s1),
peer.add(s2)
])
})
after(() => {
peer.closed().then(() => {
daemon.wsServer.close()
})
peer.close()
})
it('should be a function', () => {
assert.equal(typeof withJet, 'function')
})
it('should wrap elements', () => {
const store = configureStore(() => {})
const WithJet = withJet(state => ({}))(() => <h1>hello</h1>)
const wrapper = mount(<WithJet store={store} />)
assert.equal(wrapper.text(), 'hello')
})
it('should provide the fetchers content', done => {
const store = configureStore(combineReducers({foo: single('foo')}))
const fetchers = {
foo: {path: {startsWith: 'foo'}}
}
const connection = {url: 'ws://localhost:1999', user: 'john', password: '12345'}
const ComponentWithJet = withJet(state => ({connection}), fetchers)(Container)
const wrapper = mount(<ComponentWithJet store={store} />)
const unsubscribe = store.subscribe(() => {
if (store.getState().foo) {
assert.equal(store.getState().foo, 123)
assert.equal(wrapper.find('h1').text(), '123')
unsubscribe()
wrapper.unmount()
done()
}
})
})
it('should respect shouldRender if defined', done => {
const store = configureStore(combineReducers({foo: single('foo')}))
const fetchers = {
foo: {path: {startsWith: 'foo'}}
}
const connection = {url: 'ws://localhost:1999', user: 'john', password: '12345'}
const shouldRender = state => state.foo
const ComponentWithJet = withJet(state => ({connection}), fetchers, shouldRender)(Container)
const wrapper = mount(<ComponentWithJet store={store} />)
const unsubscribe = store.subscribe(() => {
if (!store.getState().foo) {
assert.equal(wrapper.find('h1').length, 0)
}
if (store.getState().foo) {
assert.equal(store.getState().foo, 123)
assert.equal(wrapper.find('h1').text(), '123')
unsubscribe()
wrapper.unmount()
done()
}
})
})
it('should properly connection / user change', done => {
const connection = {url: 'ws://localhost:1999', user: 'john', password: '12345'}
const store = configureStore(combineReducers({
admin: single('admin'),
foo: single('foo'),
connection: (state = connection, action) => (
action.type === 'SET_CONNECTION' ? action.connection : state
)
}))
const fetchers = {
admin: {path: {startsWith: 'admin'}},
foo: {path: {startsWith: 'foo'}}
}
const mapStateToConnection = state => ({
connection: state.connection
})
const ComponentWithJet = withJet(mapStateToConnection, fetchers)(Container)
const wrapper = mount(<ComponentWithJet store={store} />)
let wasJohn
let once
store.subscribe(() => {
const state = store.getState()
const user = state.connection.user
if (user === 'john' && state.foo) {
wasJohn = true
assert.equal(state.foo, 123)
assert(!state.admin)
assert.equal(wrapper.find('h1').text(), '123')
store.dispatch({
type: 'SET_CONNECTION',
connection: {url: 'ws://localhost:1999', user: 'admin', password: '333'}
})
} else if (user === 'admin' && state.admin && state.foo && !once) {
assert.equal(state.admin, 'secret')
assert.equal(wrapper.find('h1').text(), 'secret123')
assert(wasJohn)
once = true
done()
}
})
})
})