﻿
describe("OnLocal/RemoteChange", function() {
    beforeEach(function() {
        jasmine.Ajax.install();
        // stub initial HTTP request
        jasmine.Ajax.stubRequest(window.location.href).andReturn(TestResponses.defaultInit.success);
    });

    afterEach(function() {
        this.puppet.unobserve();
        jasmine.Ajax.uninstall();
    });

    beforeEach(function() {
        jasmine.WebSocket.install();
    });
    afterEach(function() {
        jasmine.WebSocket.uninstall();
    });
    it("should call onLocalChange callback for outgoing patches", function(done) {
        var sentSpy = jasmine.createSpy("onLocalChange");

        var puppet = this.puppet = new Puppet({
            useWebSocket: true,
            onLocalChange: sentSpy
        });
        var websocket = jasmine.WebSocket.spy.calls.mostRecent().returnValue;
        //open WS for changes
        websocket.open();
        this.puppet.obj.hello = "onLocalChange callback";

        // wait for observer and mocked initial HTTP response
        setTimeout(function() {
            expect(puppet.obj.hello).toEqual("onLocalChange callback");
            expect(sentSpy.calls.mostRecent().args[0]).toEqual([{
                "op": "replace",
                "path": "/hello",
                "value": "onLocalChange callback"
            }]);
            done();
        }, 100); // for FF
    });

    it("should call onRemoteChange callback for applied patches", function(done) {
        var receivedSpy = jasmine.createSpy("onRemoteChange");

        var puppet = this.puppet = new Puppet({
            useWebSocket: true,
            onRemoteChange: receivedSpy
        });
        var websocket = jasmine.WebSocket.spy.calls.mostRecent().returnValue;
        //open WS for changes
        websocket.open();
        websocket.onmessage({
            data: '[{"op":"replace","path":"/hello","value":"onRemoteChange callback"}]'
        });

        // wait for observer and mocked initial HTTP response
        setTimeout(function() {
            expect(puppet.obj.hello).toEqual("onRemoteChange callback");
            var mostRecentCall = receivedSpy.calls.mostRecent();
            expect(mostRecentCall.args[0]).toEqual([{
                "op": "replace",
                "path": "/hello",
                "value": "onRemoteChange callback"
            }]);
            expect(mostRecentCall.args[1]).toEqual([{removed: 'world'}]);
            done();
        }, 100); // for FF
    });

    it("should fire patch-applied event for applied patches", function(done) {
        var receivedSpy = jasmine.createSpy("patch-applied");

        var puppet = this.puppet = new Puppet({
            useWebSocket: true
        });
        puppet.addEventListener('patch-applied', receivedSpy);
        var websocket = jasmine.WebSocket.spy.calls.mostRecent().returnValue;
        //open WS for changes
        websocket.open();
        websocket.onmessage({
            data: '[{"op":"replace","path":"/hello","value":"patch-applied"}]'
        });

        // wait for observer and mocked initial HTTP response
        setTimeout(function() {
            var detail = receivedSpy.calls.mostRecent().args[0].detail;
            expect(detail.patches).toEqual([{
                "op": "replace",
                "path": "/hello",
                "value": "patch-applied"
            }]);
            expect(detail.results).toEqual([{removed: 'world'}]);
            done();
        }, 100); // for FF
    });
});

@@ -2400,34 +2400,23 @@
 oEqual(%5B
-%7Bremoved: 
 'world'
-%7D
 %5D);%0A    
@@ -3404,26 +3404,15 @@
 al(%5B
-%7Bremoved: 
 'world'
-%7D
 %5D);%0A

Fix test for simple return values of jsonpatch.apply