lundi 7 septembre 2015

Testing Component wtih Restangular object passed in view

I have component (angular directive with controller) that I am passing Restangular object so in tests I have to recreate Restangular objects and pass them to controller in makeController.

Is there a way to avoid doing Users.one(1).get().then((res) => user = res) to get Restangular user object for tests?

let $rootScope, $httpBackend, Users, makeController;

beforeEach(angular.mock.inject((_$rootScope_, _$httpBackend_, _Users_) => {
    $rootScope = _$rootScope_;
    $httpBackend = _$httpBackend_;
    Users = _Users_;

    makeController = ({user, users=[]}) => {
        let controller = new UserEditController(_Users_);
        controller.user = user;
        controller.users = users;
        return controller;
    };
}));

let user;

beforeEach(() => {
    // FIXME can it be simplified?
    $httpBackend.expectGET(/user\/1$/).respond({
        id: 1,
        name: 'User 1',
    });
    Users.one(1).get().then((res) => user = res);
    $httpBackend.flush();
})

it('should update user', () => {
    let controller = makeController({user: user});
    expect(controller.user.name).toEqual('User 1');
    controller.user.name = 'User 1 - changed';
    controller.update();
    $httpBackend.expectPUT(/users\/1$/, controller.user.plain()).respond({
        id: 1,
        name: 'User 1 - changed'
    });
    $httpBackend.flush();
});

Aucun commentaire:

Enregistrer un commentaire