UNPKG

3.15 kBJavaScriptView Raw
1var Person
2 , adapter
3 , ajaxHash
4 , ajaxType
5 , ajaxUrl
6 , expect
7 , expectData
8 , expectState
9 , expectStates
10 , expectType
11 , expectUrl
12 , people
13 , person
14 , store;
15
16expect = function(a, b) {
17 assert.equal(a, b);
18};
19
20expectUrl = function(url) {
21 expect(ajaxUrl, url);
22};
23
24expectType = function(type) {
25 expect(ajaxType, type);
26};
27
28expectData = function(hash) {
29 assert.deepEqual(ajaxHash.data, hash);
30};
31
32expectState = function(state, value, p) {
33 p = p || person;
34 if (value === undefined) {
35 value = true;
36 }
37 var flag = 'is' + state.charAt(0).toUpperCase() + state.substr(1);
38 var hasState = get(p, flag);
39 if (value === true) {
40 expect(hasState, true);
41 } else {
42 expect(hasState, false);
43 }
44};
45
46expectStates = function(state, value) {
47 people.forEach(function(person) {
48 expect(state, value, person);
49 });
50};
51
52describe('Adapter', function() {
53 beforeEach(function() {
54 ajaxUrl = undefined;
55 ajaxType = undefined;
56 ajaxHash = undefined;
57 adapter = Adapter.create({
58 ajax: function(url, type, hash) {
59 var that = this;
60 if (hash.data && type === 'GET') {
61 url += "&" + Em.$.param(hash.data);
62 }
63 var success = hash.success;
64 ajaxUrl = url;
65 ajaxType = type;
66 ajaxHash = hash;
67 if (success) {
68 hash.success = function(json) {
69 success.call(that, json);
70 };
71 }
72 }
73 });
74 store = DS.Store.create({
75 revision: DS.CURRENT_API_REVISION,
76 adapter: adapter
77 });
78 Person = DS.Model.extend({
79 name: DS.attr('string')
80 });
81 Person.toString = function() {
82 return 'App.Person';
83 };
84 });
85 afterEach(function() {
86 adapter.destroy();
87 store.destroy();
88 person = null;
89 people = null;
90 });
91 describe('#findMany', function() {
92 it('makes a GET and returns matched items', function(done) {
93 people = store.findMany(Person, ['1', '2']);
94 expectUrl('/persons');
95 expectType('POST');
96 ajaxHash.success({
97 persons: [
98 {
99 _id: '1',
100 name: 'Yehuda'
101 }, {
102 _id: '2',
103 name: 'TJ'
104 }
105 ]
106 });
107 expect(people.get('length'), 2);
108 person = people.objectAt(0);
109 expect(person.get('id'), '1');
110 person = store.find(Person, 1);
111 expect(person.get('id'), '1');
112 done();
113 });
114 });
115 describe('#findQuery', function() {
116 it('makes a POST and returns matched items', function(done) {
117 var name = 'Yehuda Katz';
118 var data = {
119 c: {
120 name: name
121 },
122 s: {
123
124 }
125 };
126 people = store.find(Person, data);
127 expectStates('loaded', false);
128 expectUrl('/persons');
129 expectType('POST');
130 ajaxHash.success({
131 persons: [
132 {
133 _id: '1',
134 name: name
135 }
136 ]
137 });
138 expect(people.get('length'), 1);
139 person = people.objectAt(0);
140 expect(person.get('id'), '1');
141 person = store.find(Person, 1);
142 expect(person.get('id'), '1');
143 done();
144 });
145 });
146});