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