|
| 1 | +import { module, test } from 'qunit'; |
| 2 | +import { setupTest } from 'website-www/tests/helpers'; |
| 3 | +import { TOAST_OPTIONS } from 'website-www/constants/toast-options'; |
| 4 | +import { APPS } from 'website-www/constants/urls'; |
| 5 | +import sinon from 'sinon'; |
| 6 | + |
| 7 | +module('Unit | Route | identity', function (hooks) { |
| 8 | + setupTest(hooks); |
| 9 | + |
| 10 | + hooks.beforeEach(function () { |
| 11 | + this.route = this.owner.lookup('route:identity'); |
| 12 | + |
| 13 | + this.route.router = { |
| 14 | + transitionTo: sinon.stub(), |
| 15 | + }; |
| 16 | + this.route.toast = { |
| 17 | + error: sinon.stub(), |
| 18 | + }; |
| 19 | + this.route.fastboot = { |
| 20 | + isFastBoot: false, |
| 21 | + }; |
| 22 | + }); |
| 23 | + |
| 24 | + test('it exists', function (assert) { |
| 25 | + assert.ok(this.route); |
| 26 | + }); |
| 27 | + |
| 28 | + test('beforeModel redirects to page-not-found when dev param is not true', function (assert) { |
| 29 | + const transition = { |
| 30 | + to: { |
| 31 | + queryParams: { |
| 32 | + dev: 'false', |
| 33 | + }, |
| 34 | + }, |
| 35 | + }; |
| 36 | + |
| 37 | + this.route.beforeModel(transition); |
| 38 | + assert.ok( |
| 39 | + this.route.router.transitionTo.calledWith('/page-not-found'), |
| 40 | + 'should redirect to page-not-found', |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + test('beforeModel allows navigation when dev param is true', function (assert) { |
| 45 | + const transition = { |
| 46 | + to: { |
| 47 | + queryParams: { |
| 48 | + dev: 'true', |
| 49 | + }, |
| 50 | + }, |
| 51 | + }; |
| 52 | + |
| 53 | + this.route.beforeModel(transition); |
| 54 | + assert.notOk(this.route.router.transitionTo.called, 'should not redirect'); |
| 55 | + }); |
| 56 | + |
| 57 | + test('model returns null in FastBoot', async function (assert) { |
| 58 | + this.route.fastboot.isFastBoot = true; |
| 59 | + const result = await this.route.model(); |
| 60 | + assert.strictEqual(result, null, 'should return null in FastBoot'); |
| 61 | + }); |
| 62 | + |
| 63 | + test('model handles 401 unauthorized response', async function (assert) { |
| 64 | + const fetchStub = sinon.stub(window, 'fetch').resolves({ |
| 65 | + ok: false, |
| 66 | + status: 401, |
| 67 | + }); |
| 68 | + |
| 69 | + const result = await this.route.model(); |
| 70 | + |
| 71 | + assert.strictEqual(result, null, 'should return null'); |
| 72 | + assert.ok( |
| 73 | + this.route.toast.error.calledWith( |
| 74 | + 'You are not logged in. Please login to continue.', |
| 75 | + '', |
| 76 | + TOAST_OPTIONS, |
| 77 | + ), |
| 78 | + 'should show error toast', |
| 79 | + ); |
| 80 | + |
| 81 | + fetchStub.restore(); |
| 82 | + }); |
| 83 | + |
| 84 | + test('model handles successful response with invalid discord role', async function (assert) { |
| 85 | + const fetchStub = sinon.stub(window, 'fetch').resolves({ |
| 86 | + ok: true, |
| 87 | + json: () => |
| 88 | + Promise.resolve({ |
| 89 | + roles: { |
| 90 | + in_discord: false, |
| 91 | + }, |
| 92 | + }), |
| 93 | + }); |
| 94 | + |
| 95 | + const result = await this.route.model(); |
| 96 | + |
| 97 | + assert.strictEqual(result, null, 'should return null'); |
| 98 | + assert.ok( |
| 99 | + this.route.router.transitionTo.calledWith('index'), |
| 100 | + 'should redirect to index', |
| 101 | + ); |
| 102 | + |
| 103 | + fetchStub.restore(); |
| 104 | + }); |
| 105 | + |
| 106 | + test('model handles network error', async function (assert) { |
| 107 | + const fetchStub = sinon |
| 108 | + .stub(window, 'fetch') |
| 109 | + .rejects(new Error('Network error')); |
| 110 | + |
| 111 | + const result = await this.route.model(); |
| 112 | + |
| 113 | + assert.deepEqual(result, null, 'should return null'); |
| 114 | + assert.ok( |
| 115 | + this.route.router.transitionTo.calledWith('index'), |
| 116 | + 'should redirect to index', |
| 117 | + ); |
| 118 | + |
| 119 | + fetchStub.restore(); |
| 120 | + }); |
| 121 | + |
| 122 | + test('model handles non-401 error response', async function (assert) { |
| 123 | + const fetchStub = sinon.stub(window, 'fetch').resolves({ |
| 124 | + ok: false, |
| 125 | + status: 500, |
| 126 | + }); |
| 127 | + |
| 128 | + const result = await this.route.model(); |
| 129 | + |
| 130 | + assert.strictEqual(result, null, 'should return null'); |
| 131 | + assert.ok( |
| 132 | + this.route.router.transitionTo.calledWith('index'), |
| 133 | + 'should redirect to index', |
| 134 | + ); |
| 135 | + |
| 136 | + fetchStub.restore(); |
| 137 | + }); |
| 138 | + |
| 139 | + test('model handles successful response with valid discord role', async function (assert) { |
| 140 | + const mockData = { |
| 141 | + roles: { |
| 142 | + in_discord: true, |
| 143 | + }, |
| 144 | + someOtherData: 'test', |
| 145 | + }; |
| 146 | + |
| 147 | + const fetchStub = sinon.stub(window, 'fetch').resolves({ |
| 148 | + ok: true, |
| 149 | + json: () => Promise.resolve(mockData), |
| 150 | + }); |
| 151 | + |
| 152 | + const result = await this.route.model(); |
| 153 | + |
| 154 | + assert.deepEqual(result, mockData, 'should return the API response data'); |
| 155 | + assert.ok(fetchStub.called, 'fetch should be called'); |
| 156 | + |
| 157 | + const [actualUrl, actualOptions] = fetchStub.firstCall.args; |
| 158 | + |
| 159 | + assert.strictEqual( |
| 160 | + actualUrl, |
| 161 | + `${APPS.API_BACKEND}/users?profile=true`, |
| 162 | + 'should call correct URL', |
| 163 | + ); |
| 164 | + |
| 165 | + assert.deepEqual( |
| 166 | + actualOptions, |
| 167 | + { |
| 168 | + credentials: 'include', |
| 169 | + headers: { |
| 170 | + Accept: 'application/json', |
| 171 | + 'Content-Type': 'application/json', |
| 172 | + }, |
| 173 | + }, |
| 174 | + 'should pass correct options', |
| 175 | + ); |
| 176 | + |
| 177 | + fetchStub.restore(); |
| 178 | + }); |
| 179 | +}); |
0 commit comments