DEV Community

Discussion on: Migrating AngularJS tests From Karma to Jest

 
abdoulayektr profile image
Abdoulaye Traoré

yo @Antonio, I hope this helps you, it's a simple example

import chai, { expect } from 'chai';
import sinonChai from 'sinon-chai';
import * as angular from 'angular';
import otListService from 'ot/components/widget/listService';
import otResourceService from 'ot/components/resourceService';
chai.use(sinonChai);

describe('otListService', function () {

  let Test;
  let _otListService;
  let httpBackend;
  let _OT_LIST_ITEMS_PER_PAGE;
  let responseArray = [{
    id: 1,
    name: 'carapuce'
  }, {
    id: 2,
    name: 'salameche'
  }, {
    id: 3,
    name: 'bulbizare'
  }];

  beforeEach(function () {
    angular.mock.module(otListService, otResourceService);
    inject(function (otResource, otListService, $httpBackend, OT_LIST_ITEMS_PER_PAGE) {
      _otListService = otListService;
      httpBackend = $httpBackend;
      Test = otResource('test');
      _OT_LIST_ITEMS_PER_PAGE = OT_LIST_ITEMS_PER_PAGE;
    });

  });

  afterEach(function () {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
  });

  describe('query()', function () {

    it('should be able to request the resource without pagination', function (done) {
      httpBackend.expectGET('test?orderBy=name&reverse=false').respond(JSON.stringify({
        models: responseArray
      }), {
        'X-Count': responseArray.length
      });

      _otListService.query(Test, null, null, null, null, null, function (data, totalItemCount) {

        data.should.be.an.instanceof(Array);
        data.should.have.length(responseArray.length);
        expect(totalItemCount).to.be.equal(responseArray.length);
        done();
      });

      httpBackend.flush();
    });
Thread Thread
 
antoniogiroz profile image
Antonio Gil

Hi!

Thanks!! very useful!

Regards!