github.com/outbrain/consul@v1.4.5/ui-v2/tests/unit/adapters/application-test.js (about) 1 import { module } from 'qunit'; 2 import test from 'ember-sinon-qunit/test-support/test'; 3 import { setupTest } from 'ember-qunit'; 4 5 module('Unit | Adapter | application', function(hooks) { 6 setupTest(hooks); 7 8 // Replace this with your real tests. 9 test('it exists', function(assert) { 10 const adapter = this.owner.lookup('adapter:application'); 11 assert.ok(adapter); 12 }); 13 test('slugFromURL returns the slug (on the assumptions its the last chunk of the url)', function(assert) { 14 const adapter = this.owner.lookup('adapter:application'); 15 const decode = this.stub().returnsArg(0); 16 const expected = 'slug'; 17 const actual = adapter.slugFromURL({ pathname: `/this/is/a/url/with/a/${expected}` }, decode); 18 assert.equal(actual, expected); 19 assert.ok(decode.calledOnce); 20 }); 21 test("uidForURL returns the a 'unique' hash for the uid using the entire url", function(assert) { 22 const adapter = this.owner.lookup('adapter:application'); 23 const hash = this.stub().returnsArg(0); 24 const expected = ['dc-1', 'slug']; 25 const url = { 26 pathname: `/this/is/a/url/with/a/${expected[1]}`, 27 searchParams: { 28 get: this.stub().returns('dc-1'), 29 }, 30 }; 31 const actual = adapter.uidForURL(url, '', hash); 32 assert.deepEqual(actual, expected); 33 assert.ok(hash.calledOnce); 34 assert.ok(url.searchParams.get.calledOnce); 35 }); 36 test("uidForURL returns the a 'unique' hash for the uid when specifying the slug", function(assert) { 37 const adapter = this.owner.lookup('adapter:application'); 38 const hash = this.stub().returnsArg(0); 39 const expected = ['dc-1', 'slug']; 40 const url = { 41 searchParams: { 42 get: this.stub().returns('dc-1'), 43 }, 44 }; 45 const actual = adapter.uidForURL(url, expected[1], hash); 46 assert.deepEqual(actual, expected); 47 assert.ok(hash.calledOnce); 48 assert.ok(url.searchParams.get.calledOnce); 49 }); 50 test("uidForURL throws an error if it can't find a datacenter on the search params", function(assert) { 51 const adapter = this.owner.lookup('adapter:application'); 52 const hash = this.stub().returnsArg(0); 53 const expected = ['dc-1', 'slug']; 54 const url = { 55 pathname: `/this/is/a/url/with/a/${expected[1]}`, 56 searchParams: { 57 get: this.stub().returns(''), 58 }, 59 }; 60 assert.throws(function() { 61 adapter.uidForURL(url, expected[1], hash); 62 }, /datacenter/); 63 assert.ok(url.searchParams.get.calledOnce); 64 }); 65 test("uidForURL throws an error if it can't find a slug", function(assert) { 66 const adapter = this.owner.lookup('adapter:application'); 67 const hash = this.stub().returnsArg(0); 68 const url = { 69 pathname: `/`, 70 searchParams: { 71 get: this.stub().returns('dc-1'), 72 }, 73 }; 74 assert.throws(function() { 75 adapter.uidForURL(url, '', hash); 76 }, /slug/); 77 assert.ok(url.searchParams.get.calledOnce); 78 }); 79 test("uidForURL throws an error if it can't find a slug", function(assert) { 80 const adapter = this.owner.lookup('adapter:application'); 81 const hash = this.stub().returnsArg(0); 82 const url = { 83 pathname: `/`, 84 searchParams: { 85 get: this.stub().returns('dc-1'), 86 }, 87 }; 88 assert.throws(function() { 89 adapter.uidForURL(url, '', hash); 90 }, /slug/); 91 assert.ok(url.searchParams.get.calledOnce); 92 }); 93 test('handleBooleanResponse returns the expected pojo structure', function(assert) { 94 const adapter = this.owner.lookup('adapter:application'); 95 adapter.uidForURL = this.stub().returnsArg(0); 96 const expected = { 97 'primary-key-name': 'url', 98 }; 99 const actual = adapter.handleBooleanResponse('url', {}, Object.keys(expected)[0], 'slug'); 100 assert.deepEqual(actual, expected); 101 assert.ok(adapter.uidForURL.calledOnce); 102 }); 103 test('handleSingleResponse returns the expected pojo structure', function(assert) { 104 const adapter = this.owner.lookup('adapter:application'); 105 const url = { 106 pathname: `/`, 107 searchParams: { 108 get: this.stub().returns('dc-1'), 109 }, 110 }; 111 adapter.uidForURL = this.stub().returns('name'); 112 const expected = { 113 Datacenter: 'dc-1', 114 Name: 'name', 115 'primary-key-name': 'name', 116 }; 117 const actual = adapter.handleSingleResponse(url, { Name: 'name' }, 'primary-key-name', 'Name'); 118 assert.deepEqual(actual, expected); 119 assert.ok(adapter.uidForURL.calledOnce); 120 }); 121 test('handleBatchResponse returns the expected pojo structure', function(assert) { 122 const adapter = this.owner.lookup('adapter:application'); 123 const url = { 124 pathname: `/`, 125 searchParams: { 126 get: this.stub().returns('dc-1'), 127 }, 128 }; 129 adapter.uidForURL = this.stub().returnsArg(1); 130 const expected = [ 131 { 132 Datacenter: 'dc-1', 133 Name: 'name1', 134 'primary-key-name': 'name1', 135 }, 136 { 137 Datacenter: 'dc-1', 138 Name: 'name2', 139 'primary-key-name': 'name2', 140 }, 141 ]; 142 const actual = adapter.handleBatchResponse( 143 url, 144 [{ Name: 'name1' }, { Name: 'name2' }], 145 'primary-key-name', 146 'Name' 147 ); 148 assert.deepEqual(actual, expected); 149 assert.ok(adapter.uidForURL.calledTwice); 150 }); 151 });