describe("Unconfigured gadget", function() {

  xit("should display prompt", function() {
  });

  xit("should expand settings", function() {
  });

  xit("should save settings", function() {
  });

});

describe("Configured gadget", function() {
  var gadget_window, getString;

  beforeEach(function() {
    loadFixtures('gadget.html');

    window.google = {};

    var contentmatch = {getContentMatches: function(){} };
    spyOn(contentmatch,'getContentMatches').andReturn( [ {subject: "An email subject"} ] );
    window.google.contentmatch = contentmatch;

    var Prefs =  function() {
      this.getString = function(key) {
	return key + '_value';
      };
    };

    gadget_window = {
      adjustHeight: jasmine.createSpy('window')
    };

    window.gadgets = {
      Prefs: Prefs,
      'window': gadget_window,
      io: {
	makeRequest: jasmine.createSpy('makeRequest'),
	ContentType: {
	  DOM: 'DOM'
	},
	MethodType: {
	  POST: 'POST',
	  PUT: 'PUT'
	},
	RequestParameters: {
	  METHOD: 'METHOD',
	  HEADERS: 'HEADERS',
	  CONTENT_TYPE: 'CONTENT_TYPE',
	  POST_DATA: 'POST_DATA'
	}
      }
    };

    window.initializeGeepivoGadget();
  });

  xit("should adjust height to 32", function() {
    expect(getString).toHaveBeenCalled();
  });

  it("should post XML story to Tracker", function() {
    $('button.create_story_button').click();

    expect(window.gadgets.io.makeRequest.callCount).toEqual(1);

    var args = window.gadgets.io.makeRequest.mostRecentCall.args;
    expect(args.shift()).toEqual('https://www.pivotaltracker.com/services/v3/projects/project_id_value/stories');

    var callback = args.shift();
    expect(typeof callback).toEqual('function');

    expect( args.shift() ).toEqual({
      METHOD: 'POST',
      HEADERS: { 'X-TrackerToken' : 'pivotal_api_token_value', 'Content-type' : 'application/xml' },
      CONTENT_TYPE: 'DOM',
      POST_DATA: "<story>\n  <project_id>project_id_value</project_id>\n  <story_type>story_type_value</story_type>\n  <name>An email subject</name>\n  <integration_id>integration_id_value</integration_id>\n  <requested_by>requested_by_value</requested_by>\n  <owned_by>owned_by_value</owned_by>\n</story>"
    });
  });

  it("should display story link when callback called with success message", function() {
    $('button.create_story_button').click();

    expect(window.gadgets.io.makeRequest.callCount).toEqual(1);
    var callback = window.gadgets.io.makeRequest.mostRecentCall.args[1];

    story_data = "<story><url>http://tracker/story/555</url><id>555</id></story>";
    response_data = {
      rc: 201,
      text: story_data
    };

    callback(response_data);

    expect( $(".notification_area").html() ).toEqual('<a href="http://tracker/story/555" target="_blank">http://tracker/story/555</a>');

  });

  xit("should make another request when callback called with success message", function() {
    $('button.create_story_button').click();

    expect(window.gadgets.io.makeRequest.callCount).toEqual(1);
    var callback = window.gadgets.io.makeRequest.mostRecentCall.args[1];

    story_data = "<story><url>http://tracker/story/555</url><id>555</id></story>";
    response_data = {
      rc: 201,
      text: story_data
    };

    callback(response_data);

    expect(window.gadgets.io.makeRequest.callCount).toEqual(2);
    var args = window.gadgets.io.makeRequest.mostRecentCall.args;

    expect(args.shift()).toEqual('https://www.pivotaltracker.com/services/v3/projects/project_id_value/stories/555');
  });


  it("should show error message callback called with error message", function() {
    $('button.create_story_button').click();

    expect(window.gadgets.io.makeRequest.callCount).toEqual(1);
    var callback = window.gadgets.io.makeRequest.mostRecentCall.args[1];

    response_data = { rc: 500, text: '' };
    callback(response_data);

    expect( $(".notification_area").html() ).toEqual('Error creating story');
  });

});

@@ -1415,32 +1415,40 @@
 al(1);%0A%0A    var 
+request_
 args = window.ga
@@ -1492,32 +1492,40 @@
 rgs;%0A    expect(
+request_
 args.shift()).to
@@ -1615,68 +1615,46 @@
 ');%0A
-%0A    var callback = args.shift();%0A    expect(typeof callback
+    expect(typeof request_args.shift()
 ).to
@@ -1668,25 +1668,24 @@
 function');%0A
-%0A
     expect( 
@@ -1684,16 +1684,24 @@
 expect( 
+request_
 args.shi
@@ -2782,33 +2782,32 @@
 a%3E');%0A%0A  %7D);%0A%0A  
-x
 it(%22should make 
@@ -3316,16 +3316,24 @@
     var 
+request_
 args = w
@@ -3394,16 +3394,24 @@
  expect(
+request_
 args.shi
@@ -3505,24 +3505,505 @@
 ries/555');%0A
+    expect(typeof request_args.shift()).toEqual('function');%0A%0A    var params = request_args.shift();%0A    var post_data = params.POST_DATA; delete(params.POST_DATA);%0A%0A    expect( params ).toEqual(%7B%0A      METHOD: 'PUT',%0A      HEADERS: %7B 'X-TrackerToken' : 'pivotal_api_token_value', 'Content-type' : 'application/xml' %7D,%0A      CONTENT_TYPE: 'DOM'%0A    %7D);%0A%0A    expect(post_data).toEqual(%22%3Cstory%3E%3Cintegration_id%3Eintegration_id_value%3C/integration_id%3E%3Cother_id%3E555%3C/other_id%3E%3C/story%3E%22);%0A
   %7D);%0A%0A%0A  it

Add spec for additional request to update other_id