/
ember-test.js
1409 lines (1206 loc) · 50.1 KB
/
ember-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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const babelEslint = require('babel-eslint');
const emberUtils = require('../../../lib/utils/ember');
const { FauxContext } = require('../../helpers/faux-context');
function parse(code) {
return babelEslint.parse(code).body[0].expression;
}
function getProperty(code) {
return parse(code).right.properties[0];
}
describe('isModule', () => {
let node;
it("should check if it's a proper module", () => {
node = parse('Ember.test()');
expect(emberUtils.isModule(node, 'test')).toBeTruthy();
node = parse('Ember.Component()');
expect(emberUtils.isModule(node, 'Component')).toBeTruthy();
node = parse('DS.test()');
expect(emberUtils.isModule(node, 'test', 'DS')).toBeTruthy();
node = parse('DS.Model()');
expect(emberUtils.isModule(node, 'Model', 'DS')).toBeTruthy();
});
});
describe('isDSModel', () => {
it("should check if it's a DS Model", () => {
const node = parse('DS.Model()');
expect(emberUtils.isDSModel(node)).toBeTruthy();
});
describe("should check if it's a DS Model even if it uses custom name", () => {
it("shouldn't detect Model when no file path is provided", () => {
const node = parse('CustomModel.extend()');
expect(emberUtils.isDSModel(node)).toBeFalsy();
});
it('should detect Model when file path is provided', () => {
const node = parse('CustomModel.extend()');
const filePath = 'example-app/models/path/to/some-model.js';
expect(emberUtils.isDSModel(node, filePath)).toBeTruthy();
});
});
});
describe('isModuleByFilePath', () => {
it('should check if current file is a component', () => {
const filePath = 'example-app/components/path/to/some-component.js';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeTruthy();
});
it('should check if current file is a component in PODs structure', () => {
const filePath = 'example-app/components/path/to/some-component/component.js';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeTruthy();
});
it('should check if current file is a controller', () => {
const filePath = 'example-app/controllers/path/to/some-feature.js';
expect(emberUtils.isModuleByFilePath(filePath, 'controller')).toBeTruthy();
});
it('should check if current file is a controller in PODs structure', () => {
const filePath = 'example-app/path/to/some-feature/controller.js';
expect(emberUtils.isModuleByFilePath(filePath, 'controller')).toBeTruthy();
});
it('should check if current file is a route', () => {
const filePath = 'example-app/routes/path/to/some-feature.js';
expect(emberUtils.isModuleByFilePath(filePath, 'route')).toBeTruthy();
});
it('should check if current file is a route - PODs structure', () => {
const filePath = 'example-app/routes/path/to/some-feature/route.js';
expect(emberUtils.isModuleByFilePath(filePath, 'route')).toBeTruthy();
});
it('should handle TypeScript files', () => {
const filePath = 'example-app/components/path/to/some-component.ts';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeTruthy();
});
// Avoid false positives:
it('should not detect a component in a folder named `fake-components`', () => {
const filePath = 'example-app/fake-components/path/to/file.js';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
});
it('should not detect a component with a file named `components`', () => {
const filePath = 'example-app/some-folder/path/to/components';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
});
it('should not detect a component with a directory named `component.js`', () => {
const filePath = 'example-app/component.js/path/to/file.js';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
});
it('should not detect a component with a directory named `component.ts`', () => {
const filePath = 'example-app/component.ts/path/to/file.js';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
});
});
describe('isMirageDirectory', () => {
it('detects the mirage directory', () => {
expect(emberUtils.isMirageDirectory('example-app/mirage/config.js')).toBeTruthy();
expect(emberUtils.isMirageDirectory('example-app/mirage/scenarios/foo.js')).toBeTruthy();
});
it('does not detect other directories', () => {
expect(emberUtils.isMirageDirectory('example-app/app/app.js')).toBeFalsy();
expect(emberUtils.isMirageDirectory('example-app/tests/test.js')).toBeFalsy();
expect(emberUtils.isMirageDirectory('example-addon/addon/addon.js')).toBeFalsy();
});
});
describe('isMirageConfig', () => {
it('detects the mirage config file', () => {
expect(emberUtils.isMirageConfig('example-app/mirage/config.js')).toBeTruthy();
});
it('does not detect other directories', () => {
expect(emberUtils.isMirageConfig('example-app/app/app.js')).toBeFalsy();
expect(emberUtils.isMirageConfig('example-app/tests/test.js')).toBeFalsy();
expect(emberUtils.isMirageConfig('example-addon/addon/addon.js')).toBeFalsy();
expect(emberUtils.isMirageConfig('example-app/mirage/scenarios/foo.js')).toBeFalsy();
});
});
describe('isTestFile', () => {
it('detects test files', () => {
expect(emberUtils.isTestFile('some-test.js')).toBeTruthy();
expect(emberUtils.isTestFile('some-test.ts')).toBeTruthy();
});
it('does not detect other files', () => {
expect(emberUtils.isTestFile('some-component.js')).toBeFalsy();
expect(emberUtils.isTestFile('my-testing-component.js')).toBeFalsy();
expect(emberUtils.isTestFile('router.js')).toBeFalsy();
expect(emberUtils.isTestFile('my-test.html')).toBeFalsy();
});
});
describe('isEmberCoreModule', () => {
it('should check if current file is a component (custom)', () => {
const context = new FauxContext(
'CustomComponent.extend()',
'example-app/components/path/to/some-component.js'
);
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberCoreModule(context, node, 'Component')).toBeTruthy();
});
it('should check if current file is a component', () => {
const context = new FauxContext(
'Component.extend()',
'example-app/some-twisted-path/some-component.js'
);
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberCoreModule(context, node, 'Component')).toBeTruthy();
});
it('should check if current file is a controller (custom)', () => {
const context = new FauxContext(
'CustomController.extend()',
'example-app/controllers/path/to/some-controller.js'
);
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberCoreModule(context, node, 'Controller')).toBeTruthy();
});
it('should check if current file is a controller', () => {
const context = new FauxContext(
'Controller.extend()',
'example-app/some-twisted-path/some-controller.js'
);
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberCoreModule(context, node, 'Controller')).toBeTruthy();
});
it('should check if current file is a route (custom)', () => {
const context = new FauxContext(
'CustomRoute.extend()',
'example-app/routes/path/to/some-route.js'
);
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberCoreModule(context, node, 'Route')).toBeTruthy();
});
it('should check if current file is a route', () => {
const context = new FauxContext(
'Route.extend()',
'example-app/some-twisted-path/some-route.js'
);
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberCoreModule(context, node, 'Route')).toBeTruthy();
});
it('should check if current file is a route with native class', () => {
const context = new FauxContext(
"import Route from '@ember/routing/route'; class MyRoute extends Route {}",
'example-app/some-twisted-path/some-route.js'
);
const node = context.ast.body[1];
expect(emberUtils.isEmberCoreModule(context, node, 'Route')).toBeTruthy();
});
it('ignores a native class with a non-identifier super class', () => {
const context = new FauxContext(
'class MyRoute extends this.ContainerObject {}',
'example-app/some-twisted-path/some-route.js'
);
const node = context.ast.body[0];
expect(emberUtils.isEmberCoreModule(context, node, 'Route')).toBeFalsy();
});
it('throws when called on wrong type of node', () => {
const context = new FauxContext('const x = 123;');
const node = context.ast.body[0];
expect(() => emberUtils.isEmberCoreModule(context, node, 'Route')).toThrow(
'Function should only be called on a `CallExpression` (classic class) or `ClassDeclaration` (native class)'
);
});
});
describe('isEmberComponent', () => {
describe("should check if it's an Ember Component", () => {
it('should detect Component when using Ember.Component', () => {
const context = new FauxContext('Ember.Component.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberComponent(context, node)).toBeTruthy();
});
it('should detect Component when using local module Component', () => {
const context = new FauxContext('Component.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberComponent(context, node)).toBeTruthy();
});
it('should detect Component when using native classes', () => {
const context = new FauxContext(`
import Component from '@ember/component';
class MyComponent extends Component {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberComponent(context, node)).toBeTruthy();
});
it('shouldnt detect Component when using native classes if the import path is incorrect', () => {
const context = new FauxContext(`
import Component from '@something-else/component';
class MyComponent extends Component {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberComponent(context, node)).toBeFalsy();
});
});
describe("should check if it's an Ember Component even if it uses custom name", () => {
it("shouldn't detect Component when no file path is provided", () => {
const context = new FauxContext('CustomComponent.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberComponent(context, node)).toBeFalsy();
});
it('should detect Component when file path is provided', () => {
const context = new FauxContext(
'CustomComponent.extend()',
'example-app/components/path/to/some-component.js'
);
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberComponent(context, node)).toBeTruthy();
});
it('should detect Component when using native classes if the import path is correct', () => {
const context = new FauxContext(`
import CustomComponent from '@ember/component';
class MyComponent extends CustomComponent {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberComponent(context, node)).toBeTruthy();
});
});
});
describe('isGlimerComponent', () => {
describe("should check if it's a Glimmer Component", () => {
it('should detect Component when using native classes', () => {
const context = new FauxContext(`
import Component from '@glimmer/component';
class MyComponent extends Component {}
`);
const node = context.ast.body[1];
expect(emberUtils.isGlimmerComponent(context, node)).toBeTruthy();
});
it('shouldnt detect Component when using native classes if the import path is incorrect', () => {
const context = new FauxContext(`
import Component from '@something-else/component';
class MyComponent extends Component {}
`);
const node = context.ast.body[1];
expect(emberUtils.isGlimmerComponent(context, node)).toBeFalsy();
});
it('shouldnt confuse Glimmer Components with Ember Components', () => {
const context = new FauxContext(`
import Component from '@ember/component';
class MyComponent extends Component {}
`);
const node = context.ast.body[1];
expect(emberUtils.isGlimmerComponent(context, node)).toBeFalsy();
});
});
describe("should check if it's a Glimmer Component even if it uses a custom name", () => {
it('should detect Component when using native classes if the import path is correct', () => {
const context = new FauxContext(`
import CustomComponent from '@glimmer/component';
class MyComponent extends CustomComponent {}
`);
const node = context.ast.body[1];
expect(emberUtils.isGlimmerComponent(context, node)).toBeTruthy();
});
});
});
describe('isEmberController', () => {
describe("should check if it's an Ember Controller", () => {
it('should detect Controller when using Ember.Controller', () => {
const context = new FauxContext('Ember.Controller.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberController(context, node)).toBeTruthy();
});
it('should detect Controller when using local module Controller', () => {
const context = new FauxContext('Controller.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberController(context, node)).toBeTruthy();
});
it('should detect Controller when using native classes', () => {
const context = new FauxContext(`
import Controller from '@ember/controller';
class MyController extends Controller {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberController(context, node)).toBeTruthy();
});
it('shouldnt detect Controller when using native classes if the import path is incorrect', () => {
const context = new FauxContext(`
import Controller from '@something-else/controller';
class MyController extends Controller {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberController(context, node)).toBeFalsy();
});
});
describe("should check if it's an Ember Controller even if it uses custom name", () => {
it("shouldn't detect Controller when no file path is provided", () => {
const context = new FauxContext('CustomController.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberController(context, node)).toBeFalsy();
});
it('should detect Controller when file path is provided', () => {
const context = new FauxContext(
'CustomController.extend()',
'example-app/controllers/path/to/some-feature.js'
);
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberController(context, node)).toBeTruthy();
});
it('should detect Controller when using native classes if the import path is correct', () => {
const context = new FauxContext(`
import CustomController from '@ember/controller';
class MyController extends CustomController {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberController(context, node)).toBeTruthy();
});
});
});
describe('isEmberRoute', () => {
describe("should check if it's an Ember Route", () => {
it('should detect Route when using Ember.Route', () => {
const context = new FauxContext('Ember.Route.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberRoute(context, node)).toBeTruthy();
});
it('should detect Route when using local module Route', () => {
const context = new FauxContext('Route.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberRoute(context, node)).toBeTruthy();
});
it('should detect Route when using native classes', () => {
const context = new FauxContext(`
import Route from '@ember/routing/route';
class MyRoute extends Route {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberRoute(context, node)).toBeTruthy();
});
it('shouldnt detect Route when using native classes if the import path is incorrect', () => {
const context = new FauxContext(`
import Route from '@something-else/routing/route';
class MyRoute extends Route {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberRoute(context, node)).toBeFalsy();
});
});
describe("should check if it's an Ember Route even if it uses custom name", () => {
it("shouldn't detect Route when no file path is provided", () => {
const context = new FauxContext('CustomRoute.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberRoute(context, node)).toBeFalsy();
});
it('should detect Route when file path is provided', () => {
const context = new FauxContext(
'CustomRoute.extend()',
'example-app/routes/path/to/some-feature.js'
);
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberRoute(context, node)).toBeTruthy();
});
it('should detect Route when using native classes if the import path is correct', () => {
const context = new FauxContext(`
import CustomRoute from '@ember/routing/route';
class MyRoute extends CustomRoute {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberRoute(context, node)).toBeTruthy();
});
});
});
describe('isEmberMixin', () => {
describe("should check if it's an Ember Mixin", () => {
it('should detect Mixin when using native classes', () => {
const context = new FauxContext(`
import Mixin from '@ember/object/mixin';
class MyMixin extends Mixin {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberMixin(context, node)).toBeTruthy();
});
it('shouldnt detect Mixin when using native classes if the import path is incorrect', () => {
const context = new FauxContext(`
import Mixin from '@something-else/object/mixin';
class MyMixin extends Mixin {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberMixin(context, node)).toBeFalsy();
});
});
describe("should check if it's an Ember Mixin even if it uses custom name", () => {
it('should detect Mixin when using native classes if the import path is correct', () => {
const context = new FauxContext(`
import CustomMixin from '@ember/object/mixin';
class MyMixin extends CustomMixin {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberMixin(context, node)).toBeTruthy();
});
});
});
describe('isEmberService', () => {
describe("should check if it's an Ember Service", () => {
it('should detect Service when using Ember.Service', () => {
const context = new FauxContext('Ember.Service.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberService(context, node)).toBeTruthy();
});
it('should detect Service when using local module Service', () => {
const context = new FauxContext('Service.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberService(context, node)).toBeTruthy();
});
it('should detect Service when using native classes', () => {
const context = new FauxContext(`
import Service from '@ember/service';
class MyService extends Service {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberService(context, node)).toBeTruthy();
});
it('shouldnt detect Service when using native classes if the import path is incorrect', () => {
const context = new FauxContext(`
import Service from '@something-else/service';
class MyService extends Service {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberService(context, node)).toBeFalsy();
});
});
describe("should check if it's an Ember Service even if it uses custom name", () => {
it("shouldn't detect Service when no file path is provided", () => {
const context = new FauxContext('CustomService.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberService(context, node)).toBeFalsy();
});
it('should detect Service when file path is provided', () => {
const context = new FauxContext(
'CustomService.extend()',
'example-app/services/path/to/some-feature.js'
);
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberService(context, node)).toBeTruthy();
});
it('should detect Service when using native classes if the import path is correct', () => {
const context = new FauxContext(`
import CustomService from '@ember/service';
class MyService extends CustomService {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberService(context, node)).toBeTruthy();
});
});
});
describe('isEmberArrayProxy', () => {
it('should detect using old module style', () => {
const context = new FauxContext('Ember.ArrayProxy.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberArrayProxy(context, node)).toBeTruthy();
});
it('should not detect using old module style with wrong name', () => {
const context = new FauxContext('Ember.SomethingElse.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberArrayProxy(context, node)).toBeFalsy();
});
it('should detect when using local module', () => {
const context = new FauxContext('ArrayProxy.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberArrayProxy(context, node)).toBeTruthy();
});
it('should not detect when using local module with wrong name', () => {
const context = new FauxContext('SomethingElse.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberArrayProxy(context, node)).toBeFalsy();
});
it('should detect when using native classes', () => {
const context = new FauxContext(`
import ArrayProxy from '@ember/array/proxy';
class MyProxy extends ArrayProxy {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberArrayProxy(context, node)).toBeTruthy();
});
it('should detect when using native classes with other name but correct import path', () => {
const context = new FauxContext(`
import OtherName from '@ember/array/proxy';
class MyProxy extends OtherName {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberArrayProxy(context, node)).toBeTruthy();
});
it('should not detect when using native classes if the import path is incorrect', () => {
const context = new FauxContext(`
import ArrayProxy from '@something-else/service';
class MyProxy extends ArrayProxy {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberArrayProxy(context, node)).toBeFalsy();
});
});
describe('isEmberObjectProxy', () => {
it('should detect using old module style', () => {
const context = new FauxContext('Ember.ObjectProxy.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberObjectProxy(context, node)).toBeTruthy();
});
it('should not detect using old module style with wrong name', () => {
const context = new FauxContext('Ember.SomethingElse.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberObjectProxy(context, node)).toBeFalsy();
});
it('should detect when using local module', () => {
const context = new FauxContext('ObjectProxy.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberObjectProxy(context, node)).toBeTruthy();
});
it('should not detect when using local module with wrong name', () => {
const context = new FauxContext('SomethingElse.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberObjectProxy(context, node)).toBeFalsy();
});
it('should detect when using native classes', () => {
const context = new FauxContext(`
import ObjectProxy from '@ember/object/proxy';
class MyProxy extends ObjectProxy {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberObjectProxy(context, node)).toBeTruthy();
});
it('should detect when using native classes with other name but correct import path', () => {
const context = new FauxContext(`
import OtherName from '@ember/object/proxy';
class MyProxy extends OtherName {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberObjectProxy(context, node)).toBeTruthy();
});
it('should not detect when using native classes if the import path is incorrect', () => {
const context = new FauxContext(`
import ObjectProxy from '@something-else/service';
class MyProxy extends ObjectProxy {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberObjectProxy(context, node)).toBeFalsy();
});
});
describe('isEmberObject', () => {
it('should detect when using local module', () => {
const context = new FauxContext('EmberObject.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberObject(context, node)).toBeTruthy();
});
it('should not detect when using local module with wrong name', () => {
const context = new FauxContext('SomethingElse.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberObject(context, node)).toBeFalsy();
});
it('should detect when using native classes', () => {
const context = new FauxContext(`
import EmberObject from '@ember/object';
class MyObject extends EmberObject {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberObject(context, node)).toBeTruthy();
});
it('should not detect when using native classes if the import path is incorrect', () => {
const context = new FauxContext(`
import EmberObject from '@something-else/object';
class MyObject extends EmberObject {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberObject(context, node)).toBeFalsy();
});
});
describe('isEmberHelper', () => {
it('should detect when using local module', () => {
const context = new FauxContext('Helper.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberHelper(context, node)).toBeTruthy();
});
it('should not detect when using local module with wrong name', () => {
const context = new FauxContext('SomethingElse.extend()');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberHelper(context, node)).toBeFalsy();
});
it('should detect when using native classes', () => {
const context = new FauxContext(`
import Helper from '@ember/component/helper';
class MyHelper extends Helper {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberHelper(context, node)).toBeTruthy();
});
it('should not detect when using native classes if the import path is incorrect', () => {
const context = new FauxContext(`
import Helper from '@something-else/component/helper';
class MyHelper extends Helper {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberHelper(context, node)).toBeFalsy();
});
});
describe('isEmberProxy', () => {
it('should detect ArrayProxy example', () => {
const context = new FauxContext(`
import ArrayProxy from '@ember/array/proxy';
class MyProxy extends ArrayProxy {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberProxy(context, node)).toBeTruthy();
});
it('should detect ObjectProxy example', () => {
const context = new FauxContext(`
import ObjectProxy from '@ember/object/proxy';
class MyProxy extends ObjectProxy {}
`);
const node = context.ast.body[1];
expect(emberUtils.isEmberProxy(context, node)).toBeTruthy();
});
it('should not detect random code', () => {
const context = new FauxContext('someFunctionCall();');
const node = context.ast.body[0].expression;
expect(emberUtils.isEmberProxy(context, node)).toBeFalsy();
});
});
describe('isInjectedServiceProp', () => {
describe('classic classes', () => {
it("should check if it's an injected service prop with renamed import", () => {
const context = new FauxContext(`
export default Controller.extend({
currentUser: service()
});
`);
const node = context.ast.body[0].declaration.arguments[0].properties[0];
expect(emberUtils.isInjectedServiceProp(node)).toBeTruthy();
});
it("should check if it's an injected service prop with full import", () => {
const context = new FauxContext(`
export default Controller.extend({
currentUser: Ember.inject.service()
});
`);
const node = context.ast.body[0].declaration.arguments[0].properties[0];
expect(emberUtils.isInjectedServiceProp(node)).toBeTruthy();
});
it("should check if it's an injected service prop with destructured import", () => {
const context = new FauxContext(`
export default Controller.extend({
currentUser: inject()
});
`);
const node = context.ast.body[0].declaration.arguments[0].properties[0];
expect(emberUtils.isInjectedServiceProp(node)).toBeTruthy();
});
it("should check that it's not an injected service prop", () => {
const context = new FauxContext(`
export default Controller.extend({
currentUser: otherFunction()
});
`);
const node = context.ast.body[0].declaration.arguments[0].properties[0];
expect(emberUtils.isInjectedServiceProp(node)).toBeFalsy();
});
it("should check that it's not an injected service prop when 'service' is not a function", () => {
const context = new FauxContext(`
export default Controller.extend({
currentUser: service.otherFunction()
});
`);
const node = context.ast.body[0].declaration.arguments[0].properties[0];
expect(emberUtils.isInjectedServiceProp(node)).toBeFalsy();
});
});
describe('native classes', () => {
it("should check if it's an injected service prop when using renamed import", () => {
const context = new FauxContext(`
class MyController extends Controller {
@service currentUser;
}
`);
const node = context.ast.body[0].body.body[0];
expect(emberUtils.isInjectedServiceProp(node)).toBeTruthy();
});
it("should check if it's an injected service prop when using decorator", () => {
const context = new FauxContext(`
class MyController extends Controller {
@inject currentUser;
}
`);
const node = context.ast.body[0].body.body[0];
expect(emberUtils.isInjectedServiceProp(node)).toBeTruthy();
});
it("should check that it's not an injected service prop when using another decorator", () => {
const context = new FauxContext(`
class MyController extends Controller {
@otherDecorator currentUser;
}
`);
const node = context.ast.body[0].body.body[0];
expect(emberUtils.isInjectedServiceProp(node)).toBeFalsy();
});
});
});
describe('isInjectedControllerProp', () => {
describe('classic classes', () => {
it("should check if it's an injected controller prop with destructed import", () => {
const context = new FauxContext(`
export default Controller.extend({
application: controller(),
});
`);
const node = context.ast.body[0].declaration.arguments[0].properties[0];
expect(emberUtils.isInjectedControllerProp(node)).toBeTruthy();
});
it("should check if it's an injected controller prop with full import", () => {
const context = new FauxContext(`
export default Controller.extend({
application: Ember.inject.controller(),
});
`);
const node = context.ast.body[0].declaration.arguments[0].properties[0];
expect(emberUtils.isInjectedControllerProp(node)).toBeTruthy();
});
});
describe('native classes', () => {
it("should check if it's an injected controller prop with decorator", () => {
const context = new FauxContext(`
class MyController extends Controller {
@controller application;
}
`);
const node = context.ast.body[0].body.body[0];
expect(emberUtils.isInjectedControllerProp(node)).toBeTruthy();
});
it("should check that it's not an injected controller prop", () => {
const context = new FauxContext(`
class MyController extends Controller {
@otherDecorator application;
}
`);
const node = context.ast.body[0].body.body[0];
expect(emberUtils.isInjectedControllerProp(node)).toBeFalsy();
});
});
});
describe('isComputedProp', () => {
let node;
it("should check if it's an computed prop", () => {
node = parse('computed()');
expect(emberUtils.isComputedProp(node)).toBeTruthy();
node = parse('Ember.computed()');
expect(emberUtils.isComputedProp(node)).toBeTruthy();
});
it('should detect allow-listed computed props with MemberExpressions', () => {
['volatile', 'meta', 'readOnly', 'property'].forEach((prop) => {
node = parse(`computed().${prop}()`);
expect(emberUtils.isComputedProp(node)).toBeTruthy();
node = parse(`Ember.computed().${prop}()`);
expect(emberUtils.isComputedProp(node)).toBeTruthy();
});
});
it("shouldn't allow other MemberExpressions", () => {
node = parse('computed().foo()');
expect(emberUtils.isComputedProp(node)).not.toBeTruthy();
node = parse('Ember.computed().foo()');
expect(emberUtils.isComputedProp(node)).not.toBeTruthy();
});
it('should detect the computed annotation', () => {
const program = babelEslint.parse('class Object { @computed() get foo() {} }');
node = program.body[0].body.body[0].decorators[0].expression;
expect(emberUtils.isComputedProp(node)).toBeTruthy();
});
it('should detect the computed annotation without parentheses', () => {
const program = babelEslint.parse('class Object { @computed get foo() {} }');
node = program.body[0].body.body[0].decorators[0].expression;
expect(emberUtils.isComputedProp(node)).toBeTruthy();
});
});
describe('isObserverProp', () => {
describe('classic classes', () => {
it("should check if it's an observer prop using destructured import", () => {
const context = new FauxContext(`
export default Controller.extend({
someObserver: observer(),
});
`);
const node = context.ast.body[0].declaration.arguments[0].properties[0];
expect(emberUtils.isObserverProp(node)).toBeTruthy();
});
it("should check if it's an observer prop with full import", () => {
const context = new FauxContext(`
export default Controller.extend({
someObserver: Ember.observer(),
});
`);
const node = context.ast.body[0].declaration.arguments[0].properties[0];
expect(emberUtils.isObserverProp(node)).toBeTruthy();
});
it("should check if it's an observer prop with multi-line observer", () => {
const context = new FauxContext(`
export default Component.extend({
levelOfHappiness: observer("attitude", "health", () => {
}),
vehicle: alias("car")
});
`);
const node = context.ast.body[0].declaration.arguments[0].properties[0];
expect(emberUtils.isObserverProp(node)).toBeTruthy();
});
});
describe('native classes', () => {
it("should check if it's an observer prop using decorator", () => {
const context = new FauxContext(`
class MyController extends Controller {
@observer someObserver;
}
`);
const node = context.ast.body[0].body.body[0];
expect(emberUtils.isObserverProp(node)).toBeTruthy();
});
it("should check if it's an observer prop using decorator with arg", () => {
const context = new FauxContext(`
class MyController extends Controller {
@observer("someArg") someObserver() {};
}
`);
const node = context.ast.body[0].body.body[0];
expect(emberUtils.isObserverProp(node)).toBeTruthy();
});
it("should check that it's not an observer prop", () => {
const context = new FauxContext(`
class MyController extends Controller {
@otherDecorator someObserver;
}
`);
const node = context.ast.body[0].body.body[0];
expect(emberUtils.isObserverProp(node)).toBeFalsy();
});
});
});
describe('isArrayProp', () => {
let node;
it('should be an array', () => {
node = getProperty('test = { test: new Ember.A() }');
expect(emberUtils.isArrayProp(node)).toBeTruthy();
node = getProperty('test = { test: new A() }');
expect(emberUtils.isArrayProp(node)).toBeTruthy();
node = getProperty('test = { test: [] }');