~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/javascript/tests/test_lp_client.js

[r=jtv,
 wallyworld][bug=824435][incr] Add tests for the request builds code
 in requestbuilds_overlay.js.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Copyright (c) 2011, Canonical Ltd. All rights reserved. */
2
2
 
3
 
YUI().use('lp.testing.runner', 'lp.testing.iorecorder', 'test', 'console',
 
3
YUI().use('lp.testing.runner', 'lp.testing.mockio', 'test', 'console',
4
4
          'lp.client', 'escape', function(Y) {
5
5
 
6
6
var Assert = Y.Assert;  // For easy access to isTrue(), etc.
30
30
            normalize("api/devel/foo/bar"), "/api/devel/foo/bar");
31
31
    },
32
32
 
 
33
    test_get_io_provider__default: function() {
 
34
        var undefined_provider,
 
35
            io_provider = Y.lp.client.get_io_provider(undefined_provider);
 
36
        Assert.areSame(Y, io_provider);
 
37
    },
 
38
 
 
39
    test_get_io_provider__mockio: function() {
 
40
        // If a mock provider is provided, it is picked as the io_provider.
 
41
        var mockio = new Y.lp.testing.mockio.MockIo(),
 
42
            io_provider = Y.lp.client.get_io_provider(mockio);
 
43
        Assert.areSame(mockio, io_provider);
 
44
    },
 
45
 
 
46
    test_get_configured_io_provider__default: function() {
 
47
        // If no io_provider is configured, Y is the io_provider.
 
48
        var io_provider = Y.lp.client.get_configured_io_provider({});
 
49
        Assert.areSame(Y, io_provider);
 
50
    },
 
51
 
 
52
    test_get_configured_io_provider__default_undefined: function() {
 
53
        // If no configuration is provided, Y is the io_provider.
 
54
        var io_provider = Y.lp.client.get_configured_io_provider();
 
55
        Assert.areSame(Y, io_provider);
 
56
    },
 
57
 
 
58
    test_get_configured_io_provider__mockio: function() {
 
59
        // If an io_provider is configured,  it is picked as the io_provider.
 
60
        var mockio = new Y.lp.testing.mockio.MockIo(),
 
61
            io_provider = Y.lp.client.get_configured_io_provider(
 
62
                {io_provider: mockio});
 
63
        Assert.areSame(mockio, io_provider);
 
64
    },
 
65
 
 
66
    test_get_configured_io_provider__different_key: function() {
 
67
        // The io_provider can be stored with a different key.
 
68
        var mockio = new Y.lp.testing.mockio.MockIo(),
 
69
            io_provider = Y.lp.client.get_configured_io_provider(
 
70
                {my_io: mockio}, 'my_io');
 
71
        Assert.areSame(mockio, io_provider);
 
72
    },
 
73
 
33
74
    test_append_qs: function() {
34
75
        var qs = "";
35
76
        qs = Y.lp.client.append_qs(qs, "Pöllä", "Perelló");
74
115
        Assert.areEqual(expected, actual);
75
116
    },
76
117
    test_load_model: function(){
77
 
        var recorder = new Y.lp.testing.iorecorder.IORecorder();
78
 
        Assert.areEqual(0, recorder.requests.length);
 
118
        var mockio = new Y.lp.testing.mockio.MockIo();
 
119
        Assert.areEqual(0, mockio.requests.length);
79
120
        var mylist = [];
80
121
        var config = {
81
 
            io: Y.bind(recorder.do_io, recorder),
 
122
            io_provider: mockio,
82
123
            on: {
83
124
                success: Y.bind(mylist.push, mylist)
84
125
            }
87
128
        var client = new Y.lp.client.Launchpad();
88
129
        var context = new Y.lp.client.Entry(client, entry_repr, null);
89
130
        Y.lp.client.load_model(context, '+myview', config);
90
 
        var request = recorder.requests[0];
91
 
        Assert.areEqual('/context/+myview/++model++', request.url);
92
 
        request.success(
93
 
            '{"boolean": true, "entry": {"resource_type_link": "foo"}}',
94
 
            {'Content-Type': 'application/json'});
 
131
        Assert.areEqual(
 
132
            '/context/+myview/++model++', mockio.last_request.url);
 
133
        mockio.success({
 
134
            responseText:
 
135
                '{"boolean": true, "entry": {"resource_type_link": "foo"}}',
 
136
            responseHeaders: {'Content-Type': 'application/json'}
 
137
        });
95
138
        var result = mylist[0];
96
139
        Assert.areSame(true, result.boolean);
97
140
        Assert.isInstanceOf(Y.lp.client.Entry, result.entry);
125
168
        Y.Assert.areNotSame(foo, bar);
126
169
    },
127
170
    test_wrap_resource_creates_mapping: function() {
128
 
        // wrap_resource creates new mappings, rather than reusing the existing
129
 
        // one.
 
171
        // wrap_resource creates new mappings, rather than reusing the
 
172
        // existing one.
130
173
        var foo = {a: 'b'};
131
174
        var bar = new Y.lp.client.Launchpad().wrap_resource(null, foo);
132
175
        Y.Assert.areNotSame(foo, bar);
294
337
    setUp: function() {
295
338
        this.client = new Y.lp.client.Launchpad();
296
339
        this.args=[this.client, null, this._on_success, false];
297
 
        this.response = new Y.lp.testing.iorecorder.MockHttpResponse();
 
340
        this.response = new Y.lp.testing.mockio.MockHttpResponse();
298
341
        this.response.setResponseHeader('Content-Type', 'application/json');
299
342
    },
300
343