Test if method removes a list item (Jasmine)

Asked

Viewed 105 times

3

I have a method in a service that removes items over 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 obtains all items from that list:

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

And the method add:

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 that tells me if I have removed the correct items from this list?

1 answer

2


There’s some code missing from the question, it’s not clear where the itens. But I’ll give you an example with this in time of itens that from what I read in 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: http://jsfiddle.net/osfwh0rh/

  • But in this case, I need to call the add by creating a fake list. With dates you would need to remove and dates you don’t need. My add already creates the "date" object with new Date(), in which case I want to pass the dates for it to create the list. This list comes from the service, it is manipulated directly there and does not contain items.

  • @Julianaprado in a test you must always have some list. As you did not give an example I created a fake. If you have a better example. Put here that I integrate or adapt the use of the code I gave...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.