-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathemployee-test.js
175 lines (152 loc) · 5.7 KB
/
employee-test.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import FactoryGuy, {
mockFindRecord,
setupFactoryGuy,
build,
make,
buildList,
} from 'ember-data-factory-guy';
import { run } from '@ember/runloop';
const modelType = 'employee';
module(`Unit | Model | ${modelType}`, function (hooks) {
setupTest(hooks);
setupFactoryGuy(hooks);
// NAME FRAGMENT
test('default employee', function (assert) {
assert.expect(2);
let employee = make('employee');
run(() => {
assert.strictEqual(employee.get('name.firstName'), 'Tyrion');
assert.strictEqual(employee.get('name.lastName'), 'Lannister');
});
});
// FRAGMENT attribute this differently named than fragment type
// passing in a value you built manually
test('making employee with attribute this differently named than fragment type', function (assert) {
assert.expect(2);
let employee = make('employee', { designation: make('name') });
run(() => {
assert.strictEqual(employee.get('designation.firstName'), 'Tyrion');
assert.strictEqual(employee.get('designation.lastName'), 'Lannister');
});
});
/** FRAGMENT attribute this differently named than fragment type
letting factory guy make the type for you from definition like:
traits: {
withDesignation:{
designation: {}
},
}
*/
test('making employee with attribute this differently named than fragment type with empty declaration in definition', function (assert) {
assert.expect(2);
let employee = make('employee', 'withDesignation');
run(() => {
assert.strictEqual(employee.get('designation.firstName'), 'Tyrion');
assert.strictEqual(employee.get('designation.lastName'), 'Lannister');
});
});
test('employee with default name trait', function (assert) {
assert.expect(2);
let employee = make('employee', 'default_name_setup');
run(() => {
assert.strictEqual(employee.get('name.firstName'), 'Tyrion');
assert.strictEqual(employee.get('name.lastName'), 'Lannister');
});
});
test('default employee with trait with custom name fragment', function (assert) {
assert.expect(2);
let employee = make('employee', 'jon');
run(() => {
assert.strictEqual(employee.get('name.firstName'), 'Jon');
assert.strictEqual(employee.get('name.lastName'), 'Snow');
});
});
test('default employee with trait with custom name belongsTo', function (assert) {
assert.expect(2);
let employee = make('employee', 'geoffrey');
run(() => {
assert.strictEqual(employee.get('name.firstName'), 'Geoffrey');
assert.strictEqual(employee.get('name.lastName'), 'Lannister');
});
});
test('make with manual setting up employee name', function (assert) {
assert.expect(1);
let firstName = 'Joe',
lastName = 'Black',
name = { firstName: 'Joe', lastName: 'Black' },
employee = make('employee', { name });
run(() => {
let names = run(() =>
employee.get('name').getProperties(['firstName', 'lastName'])
);
assert.deepEqual(names, { firstName, lastName });
});
});
test('make with manual setting up employee name ( another way )', function (assert) {
assert.expect(1);
let firstName = 'Joe',
lastName = 'Black',
name = make('name', { firstName: 'Joe', lastName: 'Black' }),
employee = make('employee', { name });
run(() => {
let names = run(() =>
employee.get('name').getProperties(['firstName', 'lastName'])
);
assert.deepEqual(names, { firstName, lastName });
});
});
// TITLES FRAGMENT
test('default employee and titles', function (assert) {
assert.expect(2);
let employee = make('employee');
run(() => {
assert.strictEqual(employee.get('titles.length'), 2);
assert.deepEqual(employee.get('titles.content'), ['Mr.', 'Dr.']);
});
});
test('build json payload by manually setting up employee name and retrieve model from store.findRecord', async function (assert) {
let firstName = 'Joe',
lastName = 'Black',
name = build('name', { firstName, lastName }).get(),
employee = build('employee', { name });
mockFindRecord('employee').returns({ json: employee });
let model = await run(async () =>
FactoryGuy.store.findRecord('employee', employee.get('id'))
);
let names = run(() =>
model.get('name').getProperties(['firstName', 'lastName'])
);
assert.deepEqual(names, { firstName, lastName });
});
// DEPARTMENT EMPLOYMENT
test('employee hasMany departmentEmployments setup manually', function (assert) {
assert.expect(1);
run(() => {
let departmentEmployments = buildList('department-employment', 2).get();
let employee = make('employee', { departmentEmployments });
assert.equal(employee.get('departmentEmployments.length'), 2);
});
});
test('employee hasMany department employments (fragment arrays) setup in fixture', function (assert) {
assert.expect(6);
let employee = make('employee', 'with_department_employments');
run(() => {
assert.equal(employee.get('departmentEmployments.length'), 2);
let department1 = employee.get(
'departmentEmployments.firstObject.department'
);
let department2 = employee
.get('departmentEmployments')
.objectAt(1)
.get('department');
assert.strictEqual(department1.get('name'), 'Acme Dept 1');
assert.strictEqual(department2.get('name'), 'Acme Dept 2');
let addresses = department1.get('addresses');
assert.strictEqual(addresses.get('length'), 3);
assert.strictEqual(addresses.get('firstObject.street'), '1 Sky Cell');
assert.strictEqual(addresses.get('lastObject.street'), '3 Sky Cell');
});
});
});