Test if method removes item from list (Jasmine)

3

I have a method in a service that removes items older than 60 days from a list.

self.removerAntigas = function () {
        var dataCorte = new Date();
        var dataAux = dataCorte.getDate();
        dataCorte.setDate(dataAux - 60);
        itens.forEach(function (item) {
            if (item.data <= dataCorte) {
                itens.splice (itens.indexOf(item),1);
            }
        });
    };

I also have the method that gets all the items in this list:

self.obterTodos = function (){
    return itens;
}

And the add method:

self.adicionar = function () {
    var item = {
        data: new Date(),
        latitude: 0,
        longitude: 0,
        conectado: true,
        sincronizado: false
    };
    itens.push(item);
};

How can I write a test using Jasmine, letting me know if I have removed the correct items from this list?

    
asked by anonymous 29.05.2015 / 18:41

1 answer

2

There is no code in the question, it is unclear where itens comes from. But I'll give an example with this instead of itens that from what I read from the code I think that's what you want.

describe('removerAntigas', function () {

    it("should add new item", function () {
        window.itens = [gerador(), gerador(), gerador()];
        window.adicionar()
        expect(window.itens.length).toEqual(4);
    });
    it("should return all items", function () {
        expect(window.obterTodos()).toEqual(window.itens);
    });
    it("should return all items", function () {
        expect(window.itens.length == 4).toBeTruthy();
        window.removerAntigas();
        expect(window.itens.length < 4).not.toBeTruthy();
    });
});

Example: link

    
29.05.2015 / 19:26