/
t16-embed-with-scope-and-array-test.coffee
76 lines (61 loc) · 2.04 KB
/
t16-embed-with-scope-and-array-test.coffee
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
should = require 'should'
_ = require 'underscore'
model = ->
targetId : ["id001","id002"]
module.exports = class ResolverUsers
constructor: () ->
# Ususally have a link to persistent store here.
@kinds = ['User'] # Supported types
resolve: (kind,userIdsToRetrieve = [],options = {},cb) =>
userIdsToRetrieve = _.uniq(userIdsToRetrieve)
result =
kind : kind
collectionName: 'users'
items : {}
if _.contains(userIdsToRetrieve,'id001')
result.items['id001'] =
id: "id001"
username: 'martin'
password: 'secret'
email: 'hello@world.com'
cb null,result
describe 'WHEN resolving stuff', ->
index = require '../lib/index'
apiFacade = index.client()
apiFacade.addSchema "TypeA",
mappings:
targetId:
name : 'targetId'
type: 'User'
collectionType: 'Array'
embed : true
resolve: false
apiFacade.addSchema "User",
mappings:
id : 'id'
username: 'username'
password: 'password'
email: 'email'
scopes:
inline:
fields: ['id','username']
scopea:
fields: ['id','username','email']
apiFacade.registerResolver new ResolverUsers
it 'IT should transform values', (done) ->
apiFacade.mapRoot 'TypeA', model(), scopes: ['scopea'], (err,jsonObj) ->
console.log "I GOT: #{JSON.stringify(jsonObj)}"
should.not.exist err
should.exist jsonObj
jsonObj.should.have.property 'targetId'
jsonObj.targetId.should.have.lengthOf 2
jsonObj.targetId[0].should.eql 'id001'
jsonObj.targetId[1].should.eql 'id002'
jsonObj.should.have.property '_embedded'
jsonObj._embedded.should.have.property 'users'
jsonObj._embedded.users.should.have.property 'id001'
jsonObj._embedded.users.id001.should.have.property 'id'
jsonObj._embedded.users.id001.should.have.property 'username'
jsonObj._embedded.users.id001.should.not.have.property 'password'
jsonObj._embedded.users.id001.should.have.property 'email'
done null