From 47be450744286550420f2287c6d719fe80bd0df8 Mon Sep 17 00:00:00 2001 From: Manuel Tanzi Date: Wed, 27 Mar 2019 09:11:34 -0700 Subject: [PATCH] Convert tests to use fetch instead of jquery --- jest.browser.json | 2 +- jest.server.json | 9 +++++++-- test/spec/fingerprint.js | 2 ++ test/spec/oauthUtil.js | 2 ++ test/spec/token.js | 2 ++ test/util/util.js | 40 ++++++++++++++++++++++++---------------- 6 files changed, 38 insertions(+), 19 deletions(-) diff --git a/jest.browser.json b/jest.browser.json index af27b09df..0ac65e486 100644 --- a/jest.browser.json +++ b/jest.browser.json @@ -3,7 +3,7 @@ "coverageDirectory": "./build2/reports/coverage", "restoreMocks": true, "moduleNameMapper": { - "^OktaAuth(.*)$": "/jquery/$1" + "^OktaAuth$": "/lib/browser/browserIndex" }, "testMatch": [ "**/test/spec/*.js" diff --git a/jest.server.json b/jest.server.json index b677696cb..3decc1061 100644 --- a/jest.server.json +++ b/jest.server.json @@ -2,13 +2,18 @@ "coverageDirectory": "./build2/reports/coverage", "restoreMocks": true, "moduleNameMapper": { - "^OktaAuth(.*)$": "/jquery/$1" + "^OktaAuth$": "/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", diff --git a/test/spec/fingerprint.js b/test/spec/fingerprint.js index c3ce542e7..0b61bf610 100644 --- a/test/spec/fingerprint.js +++ b/test/spec/fingerprint.js @@ -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 || {}; diff --git a/test/spec/oauthUtil.js b/test/spec/oauthUtil.js index 89e9ec8c4..4a317a099 100644 --- a/test/spec/oauthUtil.js +++ b/test/spec/oauthUtil.js @@ -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', diff --git a/test/spec/token.js b/test/spec/token.js index 4eb089a09..ec89221ca 100644 --- a/test/spec/token.js +++ b/test/spec/token.js @@ -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' }); } diff --git a/test/util/util.js b/test/util/util.js index 0f02bb0dc..22558b5ba 100644 --- a/test/util/util.js +++ b/test/util/util.js @@ -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 = {}; @@ -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 {