import { Component, defineRouterComponent } from '../../src/fudgel.js'; defineRouterComponent('app-router'); @Component('test-application', { template: `
Current route: {{location}}
History length: {{historyLength}}
View detail for testingId
View detail for deeper path
Do not view detail with an extra slash
Back to the default route
Default fallback route
Page 1 test links, routes, attributes
Page 2 test history and navigation Page 3 test template and nested routes
Never shown
Start Over
` }) class TestApplicationComponent { historyLength = -1; interval: ReturnType; location: string; onInit() { this.interval = setInterval(() => { this.location = window.location.pathname; this.historyLength = history.length; }, 50); } onDestroy() { clearInterval(this.interval); } } @Component('test-component', { attr: ['id'], template: ` id attribute is {{id}}
` }) class TestComponent { id; goBack() { history.back(); } } @Component('test-history', { template: ` History inits: {{inits}}
Go deeper



Back to the default route ` }) class TestHistoryComponent { history = history; inits = 0; onInit() { testHistoryInits += 1; this.inits = testHistoryInits; } } @Component('test-template', { template: ` ` }) export class TestTemplate {} let testHistoryInits = 0; describe('router', () => { beforeEach(() => { history.pushState(null, null, '/'); cy.mount(''); }); it('routes with links and sets attributes', () => { // Show the default page cy.get('#page1').should('not.exist'); cy.get('test-component').should('not.exist'); cy.get('#default').should('exist'); cy.get('#notShown').should('not.exist'); // Go to page 1 cy.get('a#page1link').click(); cy.get('#page1').should('exist'); cy.get('test-component').should('not.exist'); cy.get('#default').should('not.exist'); cy.get('#notShown').should('not.exist'); // Go to the detail component cy.get('a#testingId').click(); cy.get('#page1').should('not.exist'); cy.get('test-component').should('exist'); cy.get('#default').should('not.exist'); cy.get('#notShown').should('not.exist'); // Confirm the attribute contains the matching path segment cy.get('#id').should('have.text', 'testingId'); // Back to page 1 cy.get('button').click(); // Go to the detail component with a deeper route cy.get('a#deeper').click(); cy.get('#page1').should('not.exist'); cy.get('test-component').should('exist'); cy.get('#default').should('not.exist'); cy.get('#notShown').should('not.exist'); // Confirm the attribute only shows the first matching path segment cy.get('#id').should('have.text', 'deeper'); // Back to page 1 cy.get('button').click() // Verify that a slash is ignored at the end of a route cy.get('#slash').click(); cy.get('#location').should('have.text', '/page1/'); cy.get('#page1').should('exist'); cy.get('test-component').should('not.exist'); // Back to default route cy.get('#startOver').click(); }); it('navigates correctly with history', () => { // Confirm components do not get instantiated over and over let initsBefore; cy.get('a#page2link').click(); cy.get('#inits').then(() => { initsBefore = testHistoryInits; }); cy.get('#deeper').click(); cy.get('#deeper').click(); cy.get('#inits').then(() => { expect(testHistoryInits).to.equal(initsBefore); }); // Confirm navigation back pops from the state cy.get('#location').should('have.text', '/page2/deeper'); cy.get('button#back').click(); cy.get('#location').should('have.text', '/page2/deeper'); // not changed visibly cy.get('button#back').click(); cy.get('#location').should('have.text', '/page2'); cy.get('button#forward').click(); cy.get('#location').should('have.text', '/page2/deeper'); cy.get('button#pushState').click(); cy.get('#location').should('have.text', '/'); }); it('works with a