all files / src/app/components/user/ ReposList.spec.js

100% Statements 44/44
100% Branches 0/0
100% Functions 9/9
100% Lines 44/44
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                                                                                           
import angular from 'angular';
import 'angular-mocks';
import {ReposList} from './ReposList';
 
describe('ReposList component', () => {
  let component;
 
  /* eslint-disable camelcase */
  const repos = [{
    full_name: 'First Repo',
    language: 'FooBar',
    updated_at: new Date(new Date().valueOf() - 1000 * 60 * 1),
    stargazers_count: 1
  }, {
    full_name: 'Second Repo',
    language: 'SooBar',
    updated_at: new Date(new Date().valueOf() - 1000 * 60 * 5),
    stargazers_count: 2
  }, {
    full_name: 'Third Repo',
    language: 'SooBar',
    updated_at: new Date(new Date().valueOf() - 1000 * 60 * 4),
    stargazers_count: 2
  }, {
    full_name: 'Fourth Repo',
    language: '',
    updated_at: new Date(new Date().valueOf() - 1000 * 60 * 4),
    stargazers_count: 3
  }];
  /* eslint-enable camelcase */
 
  beforeEach(() => {
    angular.module('reposList', ['app/components/user/ReposList.html'])
      .component('reposList', ReposList);
    angular.mock.module('reposList');
  });
 
  beforeEach(() => {
    angular.mock.inject($componentController => {
      const bindings = {repos: angular.copy(repos)};
      component = $componentController('reposList', {}, bindings);
    });
  });
 
  it('should render correctly', angular.mock.inject(($rootScope, $compile) => {
    const $scope = $rootScope.$new();
    $scope.repos = repos;
    const element = $compile('<repos-list repos="repos"></repos-list>')($scope);
    $scope.$digest();
    expect(element.find('repo-item').length).toEqual(4);
  }));
 
  it('should sort on initialization', () => {
    spyOn(component, 'sortBy');
    component.$onInit();
    expect(component.sortBy).toHaveBeenCalled();
  });
 
  it('should switch sort order', () => {
    const prop = 'stargazers_count';
    spyOn(component, '_doSort');
    expect(component.active).toEqual('');
    component.sortBy(prop);
    expect(component.active).toEqual(prop);
    expect(component.reverse).toEqual(false);
    expect(component._doSort).toHaveBeenCalled();
    component.sortBy(prop);
    expect(component.reverse).toEqual(true);
    expect(component.repos[0][prop]).toEqual(1);
  });
 
  it('should sort by language', () => {
    const prop = 'language';
    component.sortBy(prop);
    expect(component.active).toEqual(prop);
    expect(component.repos[0][prop]).toEqual(repos[3][prop]);
    component.sortBy(prop);
    expect(component.repos[0][prop]).toEqual(repos[0][prop]);
  });
 
  it('should sort by date', () => {
    const prop = 'updated_at';
    component.sortBy(prop);
    expect(component.active).toEqual(prop);
    expect(component.repos[0][prop]).toEqual(repos[3][prop]);
    component.sortBy(prop);
    expect(component.repos[0][prop]).toEqual(repos[1][prop]);
  });
});