File size: 3,577 Bytes
cbc76af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { visit } from '@ember/test-helpers';
import { get } from '@ember/object';
import { run } from '@ember/runloop';
import Route from '@ember/routing/route';
import { module, test } from 'qunit';
import require from 'require';
import wait from 'ember-test-helpers/wait';
import { destroyEIApp, setupEIApp } from '../helpers/setup-destroy-ei-app';

const EmberDebug = require('ember-debug/main').default;
let port;
let App;

function setupApp() {
  this.owner.register('route:loading', Route);
  this.owner.register('route:error', Route);
}

function getChildrenProperty(route, prop) {
  return route.children.map(item => get(item.value, prop));
}

module('Ember Debug - Route Tree', function(hooks) {
  hooks.beforeEach(async function() {
    EmberDebug.Port = EmberDebug.Port.extend({
      init() {},
      send() {}
    });
    App = await setupEIApp.call(this, EmberDebug, function() {
      this.route('simple');
      this.route('posts', { resetNamespace: true });
      this.route('comments', { resetNamespace: true }, function() {
        this.route('new');
        this.route('edit', { path: '/edit/:comment_id' });
      });
    });

    setupApp.call(this);
    EmberDebug.get('generalDebug').reopen({
      emberCliConfig: null
    });
    port = EmberDebug.port;
  });

  hooks.afterEach(async function() {
    await destroyEIApp.call(this, EmberDebug, App);
  });

  test('Route tree', async function t(assert) {
    let name = null, message = null, route;
    port.reopen({
      send(n, m) {
        name = n;
        message = m;
      }
    });

    await visit('/');

    run(port, 'trigger', 'route:getTree');
    await wait();

    assert.equal(name, 'route:routeTree');

    route = message.tree.children[0];
    assert.equal(route.value.name, 'application');
    assert.equal(route.value.type, 'resource');
    assert.equal(route.value.controller.name, 'application');
    assert.equal(route.value.controller.className, 'ApplicationController');
    assert.equal(route.value.routeHandler.name, 'application');
    assert.equal(route.value.routeHandler.className, 'ApplicationRoute');
    assert.equal(route.value.template.name, 'application');
    assert.equal(route.children.length, 6);

    assert.deepEqual(getChildrenProperty(route, 'name'), ['loading', 'error', 'simple', 'posts', 'comments', 'index']);

    let commentsRoute = route.children.filter(function(child) {
      return child.value.name === 'comments';
    })[0];

    assert.ok(commentsRoute, 'expected comment steps');

    assert.equal(commentsRoute.children.length, 3);
    assert.equal(commentsRoute.value.type, 'resource');
    assert.equal(commentsRoute.value.controller.className, 'CommentsController');
    assert.equal(commentsRoute.value.routeHandler.className, 'CommentsRoute');

    assert.deepEqual(getChildrenProperty(commentsRoute, 'name'), ['comments.new', 'comments.edit', 'comments.index']);

    assert.deepEqual(getChildrenProperty(commentsRoute, 'url'), ['/comments/new', '/comments/edit/:comment_id', '/comments']);
    assert.deepEqual(getChildrenProperty(commentsRoute, 'type'), ['route', 'route', 'route']);
    assert.deepEqual(getChildrenProperty(commentsRoute, 'controller.className'), ['CommentsNewController', 'CommentsEditController', 'CommentsIndexController']);
    assert.deepEqual(getChildrenProperty(commentsRoute, 'routeHandler.className'), ['CommentsNewRoute', 'CommentsEditRoute', 'CommentsIndexRoute']);
    assert.deepEqual(getChildrenProperty(commentsRoute, 'template.name'), ['comments/new', 'comments/edit', 'comments/index']);
  });
});