You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建假路由测试导航栏组件的link-to链接与标签渲染?

Got it, let's walk through how to set up fake routes for your main-menu component integration test in Ember, so you can properly verify link URLs and label rendering. Here's a practical, step-by-step approach:

Step 1: Define Mock Routes in Your Test Setup

First, update your test setup to reset and define fake routes before initializing the router. This isolates your test from the app's real routing configuration:

moduleForComponent('main-menu', 'Integration | Component | main menu', {
  integration: true,
  setup() {
    const router = getOwner(this).lookup('router:main');
    
    // Reset the router map to avoid conflicts with app routes
    router.map(() => {});
    
    // Define your fake test routes
    router.map(function() {
      this.route('dashboard');
      this.route('user-profile');
      this.route('app-settings');
    });
    
    router.setupRouter();
  }
});
Step 2: Pass Mock Menu Data & Run Validation Tests

Next, create test-specific menu items that map to your fake routes, then render the component and verify its output:

test('renders links with correct hrefs and labels', function(assert) {
  // Mock the menu items your component expects
  const mockMenuItems = [
    { route: 'dashboard', label: 'Dashboard' },
    { route: 'user-profile', label: 'My Profile' },
    { route: 'app-settings', label: 'Settings' }
  ];

  // Pass the mock data to the component
  this.set('menuItems', mockMenuItems);
  this.render(hbs`{{main-menu items=menuItems}}`);

  // Verify labels are rendered correctly
  assert.equal(this.$('nav a').eq(0).text().trim(), 'Dashboard', 'First menu label matches expected text');
  assert.equal(this.$('nav a').eq(1).text().trim(), 'My Profile', 'Second menu label matches expected text');
  assert.equal(this.$('nav a').eq(2).text().trim(), 'Settings', 'Third menu label matches expected text');

  // Verify href attributes are generated correctly
  assert.equal(this.$('nav a').eq(0).attr('href'), '/dashboard', 'First link has correct route URL');
  assert.equal(this.$('nav a').eq(1).attr('href'), '/user-profile', 'Second link has correct route URL');
  assert.equal(this.$('nav a').eq(2).attr('href'), '/app-settings', 'Third link has correct route URL');
});
Step 3: Optional - Test Active Route Highlighting (If Applicable)

If your component adds an active state to the current route's link, you can extend the test to validate this behavior:

test('applies active class to current route link', function(assert) {
  const mockMenuItems = [
    { route: 'dashboard', label: 'Dashboard' },
    { route: 'user-profile', label: 'My Profile' }
  ];

  this.set('menuItems', mockMenuItems);
  this.render(hbs`{{main-menu items=menuItems}}`);

  // Transition to the profile route to trigger active state
  this.owner.lookup('router:main').transitionTo('user-profile');

  // Check active class presence
  assert.notOk(this.$('nav a:contains("Dashboard")').hasClass('active'), 'Inactive link does not have active class');
  assert.ok(this.$('nav a:contains("My Profile")').hasClass('active'), 'Active route link has active class');
});

This approach ensures your test is fully isolated, and you can confidently validate that your component behaves as expected with routing.

内容的提问来源于stack exchange,提问作者Alexander Koltunov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:33:54