Ho un controller con un orologio che usa il debounce da lodash per ritardare il filtraggio di un elenco di 500 ms.Come testare un orologio AngularJS usando la funzione antirimbalzo con Jasmine
$scope.$watch('filter.keywords', _.debounce(function() {
$scope.$apply(function() {
$scope.filtered = _.where(list, filter);
});
}, 500));
Sto cercando di scrivere un test che simula Jasmine inserire parole chiave di filtro che non si trovano seguiti da parole chiave che si trovano.
Il mio tentativo iniziale era di utilizzare $ digest dopo aver assegnato un nuovo valore alle parole chiave, che presumo non ha funzionato a causa del rimbalzo.
it('should filter list by reference', function() {
expect(scope.filtered).toContain(item);
scope.filter.keywords = 'rubbish';
scope.$digest();
expect(scope.filtered).not.toContain(item);
scope.filter.keywords = 'test';
scope.$digest();
expect(scope.filtered).toContain(item);
});
Così ho provato a utilizzare timeout $, ma non funziona neanche.
it('should filter list by reference', function() {
expect(scope.filtered).toContain(item);
$timeout(function() {
scope.filter.keywords = 'rubbish';
});
$timeout.flush();
expect(scope.filtered).not.toContain(item);
$timeout(function() {
scope.filter.keywords = 'test';
});
$timeout.flush();
expect(scope.filtered).toContain(item);
});
Ho anche provato a dare a $ timeout un valore superiore a 500ms impostato su debounce.
Come altri hanno risolto questo problema?
MODIFICA: ho trovato una soluzione che doveva avvolgere l'aspettativa in una funzione di timeout $ quindi chiamare $ apply sull'ambito.
it('should filter list by reference', function() {
expect(scope.filtered).toContain(item);
scope.filter.keywords = 'rubbish';
$timeout(function() {
expect(scope.filtered).not.toContain(item);
});
scope.$apply();
scope.filter.keywords = 'test';
$timeout(function() {
expect(scope.filtered).toContain(item);
});
scope.$apply();
});
Sono ancora interessato a sapere se questo approccio è migliore però.