lundi 27 juin 2016

angularjs change url in unit test

I have a controller, which reads the (optional) $state parameter:

console.info("$location.url()", $location.url());
console.info("$state.params[id]", $state.params["id"]);

..its state is defined as follows:

.state("xxx.somestate", {
    url: "/somestate?id",
    templateUrl: "page.html",
    controller: "someController",
    controllerAs: "ctrl",
    reloadOnSearch: false,
    resolve: {
    }
})

when i'm navigating to /somestate?id=xxx it's picking up the ID from the state and is working fine. The problem is I'm unsure how to unit test this scenario using $location service. What I've tried so far is:

beforeEach(() => {
    $state.params["id"] = 2;
    $location.url("somestate?id=2");
    $rootScope.$digest();
});

what i see in the console is:

INFO: '$location.url()', '/home' 
INFO: '$state.params[id]', undefined

is there a [neat] way to achieve this using URL instead of state manipulation?

Aucun commentaire:

Enregistrer un commentaire