This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/static/js/services/route-builder/route-builder.service.spec.ts
2017-01-21 22:09:22 -08:00

111 lines
No EOL
3.6 KiB
TypeScript

import { RouteBuilderImpl } from './route-builder.service.impl';
describe("Service: RouteBuilderImpl", () => {
var routeProviderMock;
var pagesMock;
var profiles;
var currentProfile;
beforeEach((() => {
profiles = [
{id: 'old-layout', templatePath: '/static/partials/'},
{id: 'layout', templatePath: '/static/partials/'}
];
currentProfile = 'layout';
routeProviderMock = jasmine.createSpyObj('routeProvider', ['otherwise', 'when']);
pagesMock = jasmine.createSpyObj('pagesMock', ['get', 'create']);
}));
describe("constructor", () => {
it("returns a RouteBuilder object", () => {
var routeBuilder = new RouteBuilderImpl(routeProviderMock, pagesMock, profiles, currentProfile);
expect(routeBuilder).toBeDefined();
});
it("sets 'profiles' to all given profiles if given current profile does not match any of the given profiles' id", () => {
var routeBuilder = new RouteBuilderImpl(routeProviderMock, pagesMock, profiles, 'fake-profile');
expect(routeBuilder.profiles).toEqual(profiles);
});
it("sets 'profiles' to the first given profile with id matching given current profile", () => {
var routeBuilder = new RouteBuilderImpl(routeProviderMock, pagesMock, profiles, currentProfile);
expect(routeBuilder.profiles).toEqual([profiles[1]]);
});
});
describe("otherwise", () => {
var routeBuilder;
beforeEach(() => {
routeBuilder = new RouteBuilderImpl(routeProviderMock, pagesMock, profiles, currentProfile);
});
it("calls routeProvider to set fallback route with given options", () => {
var options = {1: "option"};
routeBuilder.otherwise(options);
expect(routeProviderMock.otherwise.calls.argsFor(0)[0]).toEqual(options);
});
});
describe("route", () => {
var routeBuilder;
var path;
var pagename;
var page;
beforeEach(() => {
path = '/repository/:namespace/:name';
pagename = 'repo-view';
page = {
templateName: 'repository.html',
reloadOnSearch: false,
controller: jasmine.createSpy('pageController'),
flags: {},
};
routeBuilder = new RouteBuilderImpl(routeProviderMock, pagesMock, profiles, currentProfile);
});
it("calls pages with given pagename and 'profiles' to get matching page and profile pair", () => {
pagesMock.get.and.returnValue([profiles[1], page]);
routeBuilder.route(path, pagename);
expect(pagesMock.get.calls.argsFor(0)[0]).toEqual(pagename);
expect(pagesMock.get.calls.argsFor(0)[1]).toEqual(routeBuilder.profiles);
});
it("throws error if no matching page/profile pair found", () => {
pagesMock.get.and.returnValue();
try {
routeBuilder.route(path, pagename);
fail();
} catch (error) {
expect(error.message).toEqual('Unknown page: ' + pagename);
}
});
it("calls routeProvider to set route for given path and options", () => {
pagesMock.get.and.returnValue([profiles[1], page]);
var expectedOptions = {
templateUrl: profiles[1].templatePath + page.templateName,
reloadOnSearch: false,
controller: page.controller,
};
routeBuilder.route(path, pagename);
expect(routeProviderMock.when.calls.argsFor(0)[0]).toEqual(path);
expect(routeProviderMock.when.calls.argsFor(0)[1]).toEqual(expectedOptions);
});
it("returns itself (the RouteBuilder instance)", () => {
pagesMock.get.and.returnValue([profiles[1], page]);
expect(routeBuilder.route(path, pagename)).toEqual(routeBuilder);
});
});
});