Skip to content

Commit a64db9f

Browse files
committed
Tests for getId, including object ids
1 parent 4015fa5 commit a64db9f

File tree

1 file changed

+122
-79
lines changed

1 file changed

+122
-79
lines changed

test/utils.test.ts

Lines changed: 122 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ServiceState } from './service-module/types'
44
import { isNode, isBrowser } from '../src/utils'
55
import { diff as deepDiff } from 'deep-object-diff'
66
import {
7+
getId,
78
initAuth,
89
hydrateApi,
910
getServicePrefix,
@@ -18,101 +19,143 @@ import Vuex from 'vuex'
1819
Vue.use(Vuex)
1920

2021
interface RootState {
21-
auth: AuthState,
22+
auth: AuthState
2223
users: ServiceState
2324
}
2425

25-
describe('Utils', function() {
26-
before(function() {
27-
const { makeServicePlugin, makeAuthPlugin, BaseModel } = feathersVuex(
28-
feathersClient,
29-
{ serverAlias: 'utils' }
30-
)
31-
32-
class User extends BaseModel {
33-
public static modelName = 'User'
34-
public static test: boolean = true
35-
}
36-
37-
Object.assign(this, {
38-
makeServicePlugin,
39-
makeAuthPlugin,
40-
BaseModel,
41-
User
26+
describe('Utils', function () {
27+
describe('getId', () => {
28+
const idField = '_id'
29+
it('converts objects to strings', () => {
30+
const _id = { test: true }
31+
const id = getId({ _id }, idField)
32+
assert.strictEqual(typeof id, 'string')
33+
assert.strictEqual(id, _id.toString())
4234
})
43-
})
44-
it('properly populates auth', function() {
45-
const store = new Vuex.Store<RootState>({
46-
plugins: [
47-
this.makeServicePlugin({
48-
Model: this.User,
49-
servicePath: 'users',
50-
service: feathersClient.service('users')
51-
}),
52-
this.makeAuthPlugin({})
53-
]
35+
it('does not convert number ids', () => {
36+
const _id = 1
37+
const id = getId({ _id }, idField)
38+
assert.strictEqual(typeof id, 'number')
39+
assert.strictEqual(id, _id)
40+
})
41+
it('automatically finds _id', () => {
42+
const _id = 1
43+
const id = getId({ _id })
44+
assert.strictEqual(id, _id)
45+
})
46+
it('automatically finds id', () => {
47+
const referenceId = 1
48+
const id = getId({ id: referenceId })
49+
assert.strictEqual(id, referenceId)
5450
})
55-
const accessToken =
56-
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZXhwIjoiOTk5OTk5OTk5OTkiLCJuYW1lIjoiSm9obiBEb2UiLCJhZG1pbiI6dHJ1ZX0.lUlEd3xH-TnlNRbKM3jnDVTNoIg10zgzaS6QyFZE-6g'
57-
const req = {
58-
headers: {
59-
cookie: 'feathers-jwt=' + accessToken
51+
it('prefers id over _id (only due to their order in the code)', () => {
52+
const _id = 1
53+
const referenceId = 2
54+
const id = getId({ _id, id: referenceId })
55+
assert.strictEqual(id, referenceId)
56+
})
57+
})
58+
59+
describe('Auth & SSR', () => {
60+
before(function () {
61+
const {
62+
makeServicePlugin,
63+
makeAuthPlugin,
64+
BaseModel
65+
} = feathersVuex(feathersClient, { serverAlias: 'utils' })
66+
67+
class User extends BaseModel {
68+
public static modelName = 'User'
69+
public static test = true
6070
}
61-
}
62-
return initAuth({
63-
commit: store.commit,
64-
req,
65-
moduleName: 'auth',
66-
cookieName: 'feathers-jwt',
67-
feathersClient
71+
72+
Object.assign(this, {
73+
makeServicePlugin,
74+
makeAuthPlugin,
75+
BaseModel,
76+
User
77+
})
6878
})
69-
.then(() => {
70-
assert(
71-
store.state.auth.accessToken === accessToken,
72-
'the token was in place'
73-
)
74-
assert(store.state.auth.payload, 'the payload was set')
75-
return feathersClient.authentication.getAccessToken()
79+
it('properly populates auth', function () {
80+
const store = new Vuex.Store<RootState>({
81+
plugins: [
82+
this.makeServicePlugin({
83+
Model: this.User,
84+
servicePath: 'users',
85+
service: feathersClient.service('users')
86+
}),
87+
this.makeAuthPlugin({})
88+
]
7689
})
77-
.then(token => {
78-
assert.isDefined(token, 'the feathers client storage was set')
90+
const accessToken =
91+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZXhwIjoiOTk5OTk5OTk5OTkiLCJuYW1lIjoiSm9obiBEb2UiLCJhZG1pbiI6dHJ1ZX0.lUlEd3xH-TnlNRbKM3jnDVTNoIg10zgzaS6QyFZE-6g'
92+
const req = {
93+
headers: {
94+
cookie: 'feathers-jwt=' + accessToken
95+
}
96+
}
97+
return initAuth({
98+
commit: store.commit,
99+
req,
100+
moduleName: 'auth',
101+
cookieName: 'feathers-jwt',
102+
feathersClient
79103
})
80-
})
104+
.then(() => {
105+
assert(
106+
store.state.auth.accessToken === accessToken,
107+
'the token was in place'
108+
)
109+
assert(store.state.auth.payload, 'the payload was set')
110+
return feathersClient.authentication.getAccessToken()
111+
})
112+
.then(token => {
113+
assert.isDefined(token, 'the feathers client storage was set')
114+
})
115+
})
81116

82-
it('properly hydrate SSR store', function() {
83-
const { makeServicePlugin, BaseModel, models } = feathersVuex(
84-
feathersClient,
85-
{ serverAlias: 'hydrate' }
86-
)
117+
it('properly hydrate SSR store', function () {
118+
const {
119+
makeServicePlugin,
120+
BaseModel,
121+
models
122+
} = feathersVuex(feathersClient, { serverAlias: 'hydrate' })
87123

88-
class User extends BaseModel {
89-
public static modelName = 'User'
90-
public static test: boolean = true
91-
}
124+
class User extends BaseModel {
125+
public static modelName = 'User'
126+
public static test = true
127+
}
92128

93-
const store = new Vuex.Store<RootState>({
94-
plugins: [
95-
makeServicePlugin({
96-
Model: User,
97-
servicePath: 'users',
98-
service: feathersClient.service('users'),
99-
mutations: {
100-
addServerItem (state) {
101-
state.keyedById['abcdefg'] = { id: 'abcdefg', name: 'Guzz' }
129+
const store = new Vuex.Store<RootState>({
130+
plugins: [
131+
makeServicePlugin({
132+
Model: User,
133+
servicePath: 'users',
134+
service: feathersClient.service('users'),
135+
mutations: {
136+
addServerItem(state) {
137+
state.keyedById['abcdefg'] = { id: 'abcdefg', name: 'Guzz' }
138+
}
102139
}
103-
}
104-
})
105-
]
140+
})
141+
]
142+
})
143+
store.commit('users/addServerItem')
144+
assert(store.state.users.keyedById['abcdefg'], 'server document added')
145+
assert(
146+
store.state.users.keyedById['abcdefg'] instanceof Object,
147+
'server document is pure javascript object'
148+
)
149+
hydrateApi({ api: models.hydrate })
150+
assert(
151+
store.state.users.keyedById['abcdefg'] instanceof User,
152+
'document hydrated'
153+
)
106154
})
107-
store.commit('users/addServerItem')
108-
assert(store.state.users.keyedById['abcdefg'], 'server document added')
109-
assert(store.state.users.keyedById['abcdefg'] instanceof Object, 'server document is pure javascript object')
110-
hydrateApi({ api: models.hydrate })
111-
assert(store.state.users.keyedById['abcdefg'] instanceof User, 'document hydrated')
112155
})
113156

114-
describe('Inflections', function() {
115-
it('properly inflects the service prefix', function() {
157+
describe('Inflections', function () {
158+
it('properly inflects the service prefix', function () {
116159
const decisionTable = [
117160
['todos', 'todos'],
118161
['TODOS', 'tODOS'],

0 commit comments

Comments
 (0)