JavaScript How to unit test promise/callback chains in AngularJS? unit test promise,unit test promise reject,unit test promise.

I'm using a generated library (LoopBack's Angular SDK) for my model's CRUD operations, and finding it difficult to unit test controllers and services that make use of them.

Here's an example where I'm creating an organization, and upon completion of that request, I create an admin for the organization (the ID of the organization is needed, which is why I wait to create the admin).

$scope.createOrganization = function () {
  var newOrg = {
    name: $scope.organization.name,
    description: $scope.organization.description,
    location: $scope.organization.location
  };

  Organization.create(newOrg, createAdmin);
};

var createAdmin = function (organization) {
  var newAdmin = {
    name: $scope.organization.admin.name,
    email: $scope.organization.admin.email,
    password: $scope.organization.admin.password,
    organizationId: organization.id
  };

  Admin.create(newAdmin, function () {
    AuthService.login({ email: newAdmin.email, password: newAdmin.password }).then(function () {
      $state.go('admin.dashboard');
    });
  });
};

Organization.create and Admin.create are methods from the LoopBack SDK, and I'd prefer not to touch them. They don't use the typical Angular promise syntax (Service.method().then()), but instead take a callback function as a parameter (or, optionally, you can do Organization.create(newOrg).$promise.then(createAdmin).

My test for this looks something like:

it('should create an organization and admin when submitted', inject(function (Organization, Admin) {
  scope.organization = mockOrg; // object with all fields filled in
  spyOn(Organization, 'create');
  spyOn(Admin, 'create');
  scope.createOrganization();
  expect(Organization.create).toHaveBeenCalledWith({
    name: mockOrg.name,
    description: mockOrg.description,
    location: mockOrg.location
  }, Function);
}));

I'm at a loss at the end. No idea how to write a test that tests whether or not the two create functions were called with the correct information (most importantly, I need to test that the Admin is called after the Organization creation, and that it is passed an organization.id variable).

Answer:1

Does anyone know how to handle the connected, disconnected, reconnecting and etc on Laravel Echo? I'm using VueJS btw

Does anyone know how to handle the connected, disconnected, reconnecting and etc on Laravel Echo? I'm using VueJS btw

I've seen variation of this question on Stackoverflow that do not answer the question. The problem is that the background image is placed using % or center, left, top, etc. But what we actually want ...

I've seen variation of this question on Stackoverflow that do not answer the question. The problem is that the background image is placed using % or center, left, top, etc. But what we actually want ...

I am trying to convert an array into a javscript object that is designed to work with input checkboxes in AngularJS. This is the input array that I get from my backend: let selectedRolesDB = ['ADMIN'...

I am trying to convert an array into a javscript object that is designed to work with input checkboxes in AngularJS. This is the input array that I get from my backend: let selectedRolesDB = ['ADMIN'...

Facing a very silly issue. I am showing alert message on a button click. It works perfectly at first click but then it is not working on next clicks, Here is my code : http://jsfiddle.net/nre6fbm5/ ...

Facing a very silly issue. I am showing alert message on a button click. It works perfectly at first click but then it is not working on next clicks, Here is my code : http://jsfiddle.net/nre6fbm5/ ...

  1. bootstrap alert not working
  2. bootstrap alert close not working
  3. bootstrap alert dismiss not working
  4. bootstrap alert-error not working
  5. bootstrap alert class not working
  6. bootstrap alert fade not working
  7. bootstrap alert css not working
  8. bootstrap alert show not working
  9. bootstrap alert close button not working
  10. react bootstrap alert not working
  11. bootstrap 4 alert not working
  12. bootstrap alert data-dismiss not working
  13. bootstrap alert message close not working