Jelajahi Sumber

replace mockjax with xhr-mock in tests

Adam Shaw 7 tahun lalu
induk
melakukan
de8b0cf585

+ 1 - 1
karma.config.js

@@ -34,8 +34,8 @@ module.exports = function(config) {
       'tmp/test-side-effects/*.js',
 
       // dependencies for tests
+      'node_modules/xhr-mock/dist/xhr-mock.js', // TODO: should include this via require(), but .d.ts problems
       'node_modules/native-promise-only/lib/npo.src.js',
-      'node_modules/jquery-mockjax/dist/jquery.mockjax.js',
       'node_modules/jasmine-jquery/lib/jasmine-jquery.js',
       'node_modules/jasmine-fixture/dist/jasmine-fixture.js',
       'node_modules/jquery-simulate/jquery.simulate.js',

+ 27 - 32
tests/automated/legacy/event-feed-param.js

@@ -1,3 +1,4 @@
+
 describe('event feed params', function() {
 
   pushOptions({
@@ -6,24 +7,24 @@ describe('event feed params', function() {
   })
 
   beforeEach(function() {
-    $.mockjax({
-      url: '*',
-      contentType: 'text/json',
-      responseText: [
-        {
-          title: 'my event',
-          start: '2014-05-21'
-        }
-      ]
-    })
-    $.mockjaxSettings.log = function() { } // don't console.log
+    XHRMock.setup()
   })
-
   afterEach(function() {
-    $.mockjax.clear()
+    XHRMock.teardown()
   })
 
-  it('utilizes custom startParam, endParam, and timezoneParam names', function() {
+  it('utilizes custom startParam, endParam, and timezoneParam names', function(done) {
+
+    XHRMock.get(/^my-feed\.php/, function(req, res) {
+      expect(req.url().query).toEqual({
+        mystart: '2014-04-27',
+        myend: '2014-06-08',
+        currtz: 'America/Los_Angeles'
+      })
+      done()
+      return res.status(200).header('content-type', 'application/json').body('[]')
+    })
+
     initCalendar({
       events: 'my-feed.php',
       timezone: 'America/Los_Angeles',
@@ -31,16 +32,20 @@ describe('event feed params', function() {
       endParam: 'myend',
       timezoneParam: 'currtz'
     })
-    var request = $.mockjax.mockedAjaxCalls()[0]
-    expect(request.data.start).toBeUndefined()
-    expect(request.data.end).toBeUndefined()
-    expect(request.data.timezone).toBeUndefined()
-    expect(request.data.mystart).toEqual('2014-04-27')
-    expect(request.data.myend).toEqual('2014-06-08')
-    expect(request.data.currtz).toEqual('America/Los_Angeles')
   })
 
-  it('utilizes event-source-specific startParam, endParam, and timezoneParam names', function() {
+  it('utilizes event-source-specific startParam, endParam, and timezoneParam names', function(done) {
+
+    XHRMock.get(/^my-feed\.php/, function(req, res) {
+      expect(req.url().query).toEqual({
+        feedstart: '2014-04-27',
+        feedend: '2014-06-08',
+        feedctz: 'America/Los_Angeles'
+      })
+      done()
+      return res.status(200).header('content-type', 'application/json').body('[]')
+    })
+
     initCalendar({
       timezone: 'America/Los_Angeles',
       startParam: 'mystart',
@@ -55,16 +60,6 @@ describe('event feed params', function() {
         }
       ]
     })
-    var request = $.mockjax.mockedAjaxCalls()[0]
-    expect(request.data.start).toBeUndefined()
-    expect(request.data.end).toBeUndefined()
-    expect(request.data.timezone).toBeUndefined()
-    expect(request.data.mystart).toBeUndefined()
-    expect(request.data.myend).toBeUndefined()
-    expect(request.data.currtz).toBeUndefined()
-    expect(request.data.feedstart).toEqual('2014-04-27')
-    expect(request.data.feedend).toEqual('2014-06-08')
-    expect(request.data.feedctz).toEqual('America/Los_Angeles')
   })
 
 })

+ 2 - 26
tests/automated/legacy/events-gcal.js

@@ -40,8 +40,6 @@ describe('Google Calendar plugin', function() {
   })
 
   afterEach(function() {
-    $.mockjax.clear()
-    $.mockjaxSettings.log = function() { }
     console.warn = oldConsoleWarn
   })
 
@@ -50,14 +48,9 @@ describe('Google Calendar plugin', function() {
     options.events = { googleCalendarId: HOLIDAY_CALENDAR_ID }
     options.timezone = 'local'
     options.eventAfterAllRender = function() {
-      var currentRequest = $.mockjax.unmockedAjaxCalls()[0]
       var events = $('#cal').fullCalendar('clientEvents')
       var i
 
-      expect(currentRequest.data.timeMin).toEqual(REQUEST_START)
-      expect(currentRequest.data.timeMax).toEqual(REQUEST_END)
-      expect(currentRequest.data.timeZone).toBeUndefined()
-
       expect(events.length).toBe(NUM_EVENTS)
       for (i = 0; i < events.length; i++) {
         expect(events[i].url).not.toMatch('ctz=')
@@ -73,14 +66,9 @@ describe('Google Calendar plugin', function() {
     options.events = { googleCalendarId: HOLIDAY_CALENDAR_ID }
     options.timezone = 'UTC'
     options.eventAfterAllRender = function() {
-      var currentRequest = $.mockjax.unmockedAjaxCalls()[0]
       var events = $('#cal').fullCalendar('clientEvents')
       var i
 
-      expect(currentRequest.data.timeMin).toEqual(REQUEST_START)
-      expect(currentRequest.data.timeMax).toEqual(REQUEST_END)
-      expect(currentRequest.data.timeZone).toEqual('UTC')
-
       expect(events.length).toBe(NUM_EVENTS)
       for (i = 0; i < events.length; i++) {
         expect(events[i].url).toMatch('ctz=UTC')
@@ -96,14 +84,9 @@ describe('Google Calendar plugin', function() {
     options.events = { googleCalendarId: HOLIDAY_CALENDAR_ID }
     options.timezone = 'America/New York'
     options.eventAfterAllRender = function() {
-      var currentRequest = $.mockjax.unmockedAjaxCalls()[0]
       var events = $('#cal').fullCalendar('clientEvents')
       var i
 
-      expect(currentRequest.data.timeMin).toEqual(REQUEST_START)
-      expect(currentRequest.data.timeMax).toEqual(REQUEST_END)
-      expect(currentRequest.data.timeZone).toEqual('America/New_York') // space should be escaped
-
       expect(events.length).toBe(NUM_EVENTS)
       for (i = 0; i < events.length; i++) {
         expect(events[i].url).toMatch('ctz=America/New_York')
@@ -118,15 +101,10 @@ describe('Google Calendar plugin', function() {
     options.googleCalendarApiKey = API_KEY
     options.events = { googleCalendarId: HOLIDAY_CALENDAR_ID }
     options.eventAfterAllRender = function() {
-      var currentRequest = $.mockjax.unmockedAjaxCalls()[0]
       var events = $('#cal').fullCalendar('clientEvents')
       var eventEls = $('.fc-event')
       var i
 
-      expect(currentRequest.data.timeMin).toEqual(REQUEST_START)
-      expect(currentRequest.data.timeMax).toEqual(REQUEST_END)
-      expect(currentRequest.data.timeZone).toBeUndefined()
-
       expect(events.length).toBe(NUM_EVENTS) // 5 holidays in November 2016 (and end of Oct)
       for (i = 0; i < events.length; i++) {
         expect(events[i].url).not.toMatch('ctz=')
@@ -180,14 +158,13 @@ describe('Google Calendar plugin', function() {
         googleCalendarId: HOLIDAY_CALENDAR_ID
       }
       options.eventAfterAllRender = function() {
-        var currentRequest = $.mockjax.unmockedAjaxCalls()[0]
         var events = $('#cal').fullCalendar('clientEvents')
 
         expect(events.length).toBe(0)
         expect(currentWarnArgs.length).toBeGreaterThan(0)
         expect(options.googleCalendarError).toHaveBeenCalled()
         expect(options.events.googleCalendarError).toHaveBeenCalled()
-        expect(currentRequest).toBeUndefined() // AJAX request should have never been made!
+
         done()
       }
       spyOn(options, 'googleCalendarError').and.callThrough()
@@ -209,14 +186,13 @@ describe('Google Calendar plugin', function() {
         googleCalendarId: HOLIDAY_CALENDAR_ID
       }
       options.eventAfterAllRender = function() {
-        var currentRequest = $.mockjax.unmockedAjaxCalls()[0]
         var events = $('#cal').fullCalendar('clientEvents')
 
         expect(events.length).toBe(0)
         expect(currentWarnArgs.length).toBeGreaterThan(0)
         expect(options.googleCalendarError).toHaveBeenCalled()
         expect(options.events.googleCalendarError).toHaveBeenCalled()
-        expect(typeof currentRequest).toBe('object') // request should have been sent
+
         done()
       }
       spyOn(options, 'googleCalendarError').and.callThrough()

+ 108 - 58
tests/automated/legacy/events-json-feed.js

@@ -6,78 +6,107 @@ describe('events as a json feed', function() {
   })
 
   beforeEach(function() {
-    $.mockjax({
-      url: '/my-feed.php',
-      contentType: 'text/json',
-      responseText: [
-        {
-          title: 'my event',
-          start: '2014-05-21'
-        }
-      ]
-    })
-    $.mockjaxSettings.log = function() { } // don't console.log
+    XHRMock.setup()
   })
 
   afterEach(function() {
-    $.mockjax.clear()
+    XHRMock.teardown()
   })
 
-  it('requests correctly when no timezone', function() {
+  it('requests correctly when no timezone', function(done) {
+
+    XHRMock.get(/^my-feed\.php/, function(req, res) {
+      expect(req.url().query).toEqual({
+        start: '2014-04-27',
+        end: '2014-06-08'
+      })
+      done()
+      return res.status(200).header('content-type', 'application/json').body('[]')
+    })
+
     initCalendar({
-      events: '/my-feed.php'
+      events: 'my-feed.php'
     })
-    var request = $.mockjax.mockedAjaxCalls()[0]
-    expect(request.data.start).toEqual('2014-04-27')
-    expect(request.data.end).toEqual('2014-06-08')
-    expect(request.data.timezone).toBeUndefined()
   })
 
-  it('requests correctly when local timezone', function() {
+  it('requests correctly when local timezone', function(done) {
+
+    XHRMock.get(/^my-feed\.php/, function(req, res) {
+      expect(req.url().query).toEqual({
+        start: '2014-04-27',
+        end: '2014-06-08'
+      })
+      done()
+      return res.status(200).header('content-type', 'application/json').body('[]')
+    })
+
     initCalendar({
-      events: '/my-feed.php',
+      events: 'my-feed.php',
       timezone: 'local'
     })
-    var request = $.mockjax.mockedAjaxCalls()[0]
-    expect(request.data.start).toEqual('2014-04-27')
-    expect(request.data.end).toEqual('2014-06-08')
-    expect(request.data.timezone).toBeUndefined()
   })
 
-  it('requests correctly when UTC timezone', function() {
+  it('requests correctly when UTC timezone', function(done) {
+
+    XHRMock.get(/^my-feed\.php/, function(req, res) {
+      expect(req.url().query).toEqual({
+        start: '2014-04-27',
+        end: '2014-06-08',
+        timezone: 'UTC'
+      })
+      done()
+      return res.status(200).header('content-type', 'application/json').body('[]')
+    })
+
     initCalendar({
-      events: '/my-feed.php',
+      events: 'my-feed.php',
       timezone: 'UTC'
     })
-    var request = $.mockjax.mockedAjaxCalls()[0]
-    expect(request.data.start).toEqual('2014-04-27')
-    expect(request.data.end).toEqual('2014-06-08')
-    expect(request.data.timezone).toEqual('UTC')
   })
 
-  it('requests correctly when custom timezone', function() {
+  it('requests correctly when custom timezone', function(done) {
+
+    XHRMock.get(/^my-feed\.php/, function(req, res) {
+      expect(req.url().query).toEqual({
+        start: '2014-04-27',
+        end: '2014-06-08',
+        timezone: 'America/Chicago'
+      })
+      done()
+      return res.status(200).header('content-type', 'application/json').body('[]')
+    })
+
     initCalendar({
-      events: '/my-feed.php',
+      events: 'my-feed.php',
       timezone: 'America/Chicago'
     })
-    var request = $.mockjax.mockedAjaxCalls()[0]
-    expect(request.data.start).toEqual('2014-04-27')
-    expect(request.data.end).toEqual('2014-06-08')
-    expect(request.data.timezone).toEqual('America/Chicago')
   })
 
   it('requests correctly with event source extended form', function(done) {
+
+    XHRMock.get(/^my-feed\.php/, function(req, res) {
+      expect(req.url().query).toEqual({
+        start: '2014-04-27',
+        end: '2014-06-08',
+        timezone: 'America/Chicago'
+      })
+      return res.status(200).header('content-type', 'application/json').body(
+        JSON.stringify([
+          {
+            title: 'my event',
+            start: '2014-05-21'
+          }
+        ])
+      )
+    })
+
     initCalendar({
       eventSources: [ {
-        url: '/my-feed.php',
+        url: 'my-feed.php',
         className: 'customeventclass'
       } ],
       timezone: 'America/Chicago',
       eventRender: function(eventObj, eventElm) {
-        var request = $.mockjax.mockedAjaxCalls()[0]
-        expect(request.data.start).toEqual('2014-04-27')
-        expect(request.data.end).toEqual('2014-06-08')
-        expect(request.data.timezone).toEqual('America/Chicago')
         expect(eventElm).toHaveClass('customeventclass')
         done()
       }
@@ -85,43 +114,60 @@ describe('events as a json feed', function() {
   })
 
   it('accepts jQuery.ajax params', function(done) {
+
+    XHRMock.get(/^my-feed\.php/, function(req, res) {
+      expect(req.url().query).toEqual({
+        start: '2014-04-27',
+        end: '2014-06-08',
+        customParam: 'yes'
+      })
+      done()
+      return res.status(200).header('content-type', 'application/json').body('[]')
+    })
+
     initCalendar({
       eventSources: [ {
-        url: '/my-feed.php',
+        url: 'my-feed.php',
         data: {
           customParam: 'yes'
         },
-        success: function() {
-          var request = $.mockjax.mockedAjaxCalls()[0]
-          expect(request.data.customParam).toMatch('yes')
-          done()
-        }
       } ]
     })
   })
 
   it('accepts a dynamic data function', function(done) {
+
+    XHRMock.get(/^my-feed\.php/, function(req, res) {
+      expect(req.url().query).toEqual({
+        start: '2014-04-27',
+        end: '2014-06-08',
+        customParam: 'heckyeah'
+      })
+      done()
+      return res.status(200).header('content-type', 'application/json').body('[]')
+    })
+
     initCalendar({
       eventSources: [ {
-        url: '/my-feed.php',
+        url: 'my-feed.php',
         data: function() {
           return {
             customParam: 'heckyeah'
           }
         }
-      } ],
-      eventAfterAllRender: function() {
-        var request = $.mockjax.mockedAjaxCalls()[0]
-        expect(request.data.customParam).toMatch('heckyeah')
-        done()
-      }
+      } ]
     })
   })
 
   it('calls loading callback', function(done) {
     var loadingCallArgs = []
+
+    XHRMock.get(/^my-feed\.php/, function(req, res) {
+      return res.status(200).header('content-type', 'application/json').body('[]')
+    })
+
     initCalendar({
-      events: { url: '/my-feed.php' },
+      events: { url: 'my-feed.php' },
       loading: function(bool) {
         loadingCallArgs.push(bool)
       },
@@ -133,11 +179,15 @@ describe('events as a json feed', function() {
   })
 
   it('has and Event Source object with certain props', function() {
-    var url = '/my-feed.php'
+
+    XHRMock.get(/^my-feed\.php/, function(req, res) {
+      return res.status(200).header('content-type', 'application/json').body('[]')
+    })
+
     initCalendar({
-      events: { url: url }
+      events: { url: 'my-feed.php' }
     })
-    expect(currentCalendar.getEventSources()[0].url).toBe(url)
+    expect(currentCalendar.getEventSources()[0].url).toBe('my-feed.php')
   })
 
 })

+ 7 - 6
tests/automated/legacy/removeEventSource.js

@@ -6,16 +6,17 @@ describe('removeEventSource', function() {
     options = {
       defaultDate: '2014-08-01'
     }
-    $.mockjax({
-      url: '*',
-      contentType: 'text/json',
-      responseText: buildEventArray()
+
+    XHRMock.setup()
+    XHRMock.get(/.*/, {
+      status: 200,
+      headers: { 'content-type': 'application/json' },
+      body: JSON.stringify(buildEventArray())
     })
-    $.mockjaxSettings.log = function() { } // don't console.log
   })
 
   afterEach(function() {
-    $.mockjax.clear()
+    XHRMock.teardown()
   })
 
   describe('with a URL', function() {