Skip to content

Commit

Permalink
Convert tests to use fetch instead of jquery
Browse files Browse the repository at this point in the history
  • Loading branch information
manueltanzi-okta committed Mar 27, 2019
1 parent a458ce2 commit 47be450
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion jest.browser.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"coverageDirectory": "./build2/reports/coverage",
"restoreMocks": true,
"moduleNameMapper": {
"^OktaAuth(.*)$": "<rootDir>/jquery/$1"
"^OktaAuth$": "<rootDir>/lib/browser/browserIndex"
},
"testMatch": [
"**/test/spec/*.js"
Expand Down
9 changes: 7 additions & 2 deletions jest.server.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
"coverageDirectory": "./build2/reports/coverage",
"restoreMocks": true,
"moduleNameMapper": {
"^OktaAuth(.*)$": "<rootDir>/jquery/$1"
"^OktaAuth$": "<rootDir>/lib/server/serverIndex"
},
"testMatch": [
"**/test/spec/*.js"
],
"testPathIgnorePatterns": [
"./test/spec/oauthUtil.js"
"./test/spec/fingerprint.js",
"./test/spec/general.js",
"./test/spec/oauthUtil.js",
"./test/spec/token.js",
"./test/spec/tokenManager.js",
"./test/spec/webfinger.js"
],
"reporters": [
"default",
Expand Down
2 changes: 2 additions & 0 deletions test/spec/fingerprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var OktaAuth = require('OktaAuth');
var util = require('../util/util');
var packageJson = require('../../package.json');

jest.mock('cross-fetch');

describe('fingerprint', function() {
function setup(options) {
options = options || {};
Expand Down
2 changes: 2 additions & 0 deletions test/spec/oauthUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var wellKnown = require('../xhr/well-known');
var keys = require('../xhr/keys');
var tokens = require('../util/tokens');

jest.mock('cross-fetch');

describe('getWellKnown', function() {
util.itMakesCorrectRequestResponse({
title: 'caches response and uses cache on subsequent requests',
Expand Down
2 changes: 2 additions & 0 deletions test/spec/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var packageJson = require('../../package.json');
var _ = require('lodash');
var Q = require('q');

jest.mock('cross-fetch');

function setupSync() {
return new OktaAuth({ issuer: 'http://example.okta.com' });
}
Expand Down
40 changes: 24 additions & 16 deletions test/util/util.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* globals expect, JSON */
/* eslint-disable max-statements, complexity */
var Q = require('q'),
$ = require('jquery'),
_ = require('lodash'),
OktaAuth = require('OktaAuth'),
cookies = require('../../lib/browser/browserStorage').storage;
cookies = require('../../lib/browser/browserStorage').storage,
fetch = require('cross-fetch');

jest.mock('cross-fetch');

var util = {};

Expand Down Expand Up @@ -80,44 +82,50 @@ function mockAjax(pairs) {
setNextPair(pairs);
}

jest.spyOn($, 'ajax').mockImplementation(function(args) {

fetch.mockImplementation(function (url, args) {
var pair = allPairs.shift();
if (!pair) {
throw new Error('We are making a request that we have not anticipated.');
}

// Make sure every request is attaching cookies
expect(args.xhrFields).toEqual({
withCredentials: true
});
expect(args.credentials).toEqual('include');

if (pair.request) {
expect(pair.request.uri).toEqual(args.url);
if (pair.request.data || args.data) {
expect(pair.request.data).toEqual(JSON.parse(args.data));
expect(pair.request.uri).toEqual(url);
if (pair.request.data || args.body) {
expect(pair.request.data).toEqual(JSON.parse(args.body));
}
if (pair.request.headers) {
expect(pair.request.headers).toEqual(args.headers);
}
}

var deferred = $.Deferred();
var deferred = Q.defer();
var xhr = pair.response;
xhr.headers = xhr.headers || {};
xhr.headers['Content-Type'] = 'application/json';
xhr.headers.get = function(attr) {
return xhr.headers[attr];
}
xhr.ok = xhr.status >= 200 && xhr.status < 300;
xhr.json = function() {
return Q.Promise(function(resolve) {
resolve(xhr.responseText);
});
}

xhr.getResponseHeader = function(name) {
return xhr.headers && xhr.headers[name];
};

if (xhr.status > 0 && xhr.status < 300) {
// $.ajax send (data, textStatus, jqXHR) on success
_.defer(function () { deferred.resolve(xhr.response, null, xhr); });
_.defer(function () { deferred.resolve(xhr); });
} else {
// $.ajax send (jqXHR, textStatus, errorThrown) on failure
xhr.responseJSON = xhr.response;
deferred.reject(xhr, null, xhr.response);
deferred.reject(xhr);
}
return deferred;
return deferred.promise;
});

return {
Expand Down

0 comments on commit 47be450

Please sign in to comment.