|
| 1 | +import { click, currentURL } from '@ember/test-helpers'; |
| 2 | +import { module, test } from 'qunit'; |
| 3 | + |
| 4 | +import Service from '@ember/service'; |
| 5 | + |
| 6 | +import { setupApplicationTest } from 'crates-io/tests/helpers'; |
| 7 | + |
| 8 | +import { visit } from '../helpers/visit-ignoring-abort'; |
| 9 | + |
| 10 | +module('Route | support', function (hooks) { |
| 11 | + setupApplicationTest(hooks); |
| 12 | + |
| 13 | + test('should not retain query params when exiting and then returning', async function (assert) { |
| 14 | + await visit('/support?inquire=crate-violation'); |
| 15 | + assert.strictEqual(currentURL(), '/support?inquire=crate-violation'); |
| 16 | + assert |
| 17 | + .dom('[data-test-id="support-main-content"] section') |
| 18 | + .exists({ count: 1 }) |
| 19 | + .hasAttribute('data-test-id', 'crate-violation-section'); |
| 20 | + |
| 21 | + // back to index |
| 22 | + await click('header [href="/"]'); |
| 23 | + assert.strictEqual(currentURL(), '/'); |
| 24 | + assert.dom('footer [href="/support"]').exists(); |
| 25 | + |
| 26 | + // goto support |
| 27 | + await click('footer [href="/support"]'); |
| 28 | + assert.strictEqual(currentURL(), '/support'); |
| 29 | + assert |
| 30 | + .dom('[data-test-id="support-main-content"] section') |
| 31 | + .exists({ count: 1 }) |
| 32 | + .hasAttribute('data-test-id', 'inquire-list-section'); |
| 33 | + await click('[data-test-id="link-crate-violation"]'); |
| 34 | + assert.strictEqual(currentURL(), '/support?inquire=crate-violation'); |
| 35 | + }); |
| 36 | + |
| 37 | + test('LinkTo support must overwite query', async function (assert) { |
| 38 | + // query params of LinkTo support's in footer will not be cleared |
| 39 | + class MockService extends Service { |
| 40 | + paramsFor() { |
| 41 | + return {}; |
| 42 | + } |
| 43 | + } |
| 44 | + this.owner.register('service:pristine-query', MockService); |
| 45 | + |
| 46 | + await visit('/support?inquire=crate-violation'); |
| 47 | + assert.strictEqual(currentURL(), '/support?inquire=crate-violation'); |
| 48 | + assert |
| 49 | + .dom('[data-test-id="support-main-content"] section') |
| 50 | + .exists({ count: 1 }) |
| 51 | + .hasAttribute('data-test-id', 'crate-violation-section'); |
| 52 | + // without overwriting, link in footer will contain the query params in support route |
| 53 | + assert.dom('footer [href^="/support"]').doesNotMatchSelector('[href="/support"]'); |
| 54 | + assert.dom('footer [href^="/support"]').hasAttribute('href', '/support?inquire=crate-violation'); |
| 55 | + |
| 56 | + // back to index |
| 57 | + await click('header [href="/"]'); |
| 58 | + assert.strictEqual(currentURL(), '/'); |
| 59 | + assert.dom('footer [href^="/support"]').hasAttribute('href', '/support'); |
| 60 | + }); |
| 61 | + |
| 62 | + test('must reset query when existing', async function (assert) { |
| 63 | + const route = this.owner.lookup('route:support'); |
| 64 | + const originResetController = route.resetController; |
| 65 | + // query params of LinkTo support's in footer will not be cleared |
| 66 | + class MockService extends Service { |
| 67 | + paramsFor() { |
| 68 | + return {}; |
| 69 | + } |
| 70 | + } |
| 71 | + this.owner.register('service:pristine-query', MockService); |
| 72 | + // exiting support will not reset query |
| 73 | + route.resetController = () => {}; |
| 74 | + |
| 75 | + await visit('/support?inquire=crate-violation'); |
| 76 | + assert.strictEqual(currentURL(), '/support?inquire=crate-violation'); |
| 77 | + assert |
| 78 | + .dom('[data-test-id="support-main-content"] section') |
| 79 | + .exists({ count: 1 }) |
| 80 | + .hasAttribute('data-test-id', 'crate-violation-section'); |
| 81 | + |
| 82 | + // back to index |
| 83 | + await click('header [href="/"]'); |
| 84 | + assert.strictEqual(currentURL(), '/'); |
| 85 | + // without resetController to reset, link in footer will contain the query params in other route |
| 86 | + assert.dom('footer [href^="/support"]').doesNotMatchSelector('[href="/support"]'); |
| 87 | + assert.dom('footer [href^="/support"]').hasAttribute('href', '/support?inquire=crate-violation'); |
| 88 | + |
| 89 | + route.resetController = originResetController; |
| 90 | + }); |
| 91 | +}); |
0 commit comments