|
| 1 | +import { module, test } from 'qunit'; |
| 2 | +import { setupTest } from 'croodle/tests/helpers'; |
| 3 | +import Poll from 'croodle/models/poll'; |
| 4 | + |
| 5 | +function generateMockPoll(overwrites?: Partial<Poll>): Poll { |
| 6 | + return new Poll({ |
| 7 | + anonymousUser: false, |
| 8 | + answerType: 'YesNo', |
| 9 | + creationDate: '2024-12-31T00:00:00Z', |
| 10 | + description: 'dummy data', |
| 11 | + expirationDate: '2025-05-01T00:00:00Z', |
| 12 | + forceAnswer: true, |
| 13 | + id: 'abc', |
| 14 | + options: [{ title: 'foo' }], |
| 15 | + pollType: 'FindADate', |
| 16 | + timezone: null, |
| 17 | + title: 'Dummy', |
| 18 | + users: [], |
| 19 | + version: '1', |
| 20 | + ...overwrites, |
| 21 | + }); |
| 22 | +} |
| 23 | + |
| 24 | +module('Unit | Service | poll-settings', function (hooks) { |
| 25 | + setupTest(hooks); |
| 26 | + |
| 27 | + module('PollsettingsService', function () { |
| 28 | + module('getSettings', function () { |
| 29 | + test('returns same settings for the same poll', function (assert) { |
| 30 | + const service = this.owner.lookup('service:poll-settings'); |
| 31 | + const poll = generateMockPoll(); |
| 32 | + |
| 33 | + assert.strictEqual( |
| 34 | + service.getSettings(poll), |
| 35 | + service.getSettings(poll), |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + test('returns different settings for different poll', function (assert) { |
| 40 | + const service = this.owner.lookup('service:poll-settings'); |
| 41 | + const pollA = generateMockPoll(); |
| 42 | + const pollB = generateMockPoll(); |
| 43 | + |
| 44 | + assert.notStrictEqual( |
| 45 | + service.getSettings(pollA), |
| 46 | + service.getSettings(pollB), |
| 47 | + ); |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + module('PollSettings', function () { |
| 53 | + module('mustChooseTimeZone', function () { |
| 54 | + test('false if poll time zone is not set', function (assert) { |
| 55 | + const service = this.owner.lookup('service:poll-settings'); |
| 56 | + const poll = generateMockPoll(); |
| 57 | + const settings = service.getSettings(poll); |
| 58 | + |
| 59 | + assert.false(settings.mustChooseTimeZone); |
| 60 | + }); |
| 61 | + |
| 62 | + test("false if poll's time zone equals user's time zone", function (assert) { |
| 63 | + const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; |
| 64 | + |
| 65 | + const service = this.owner.lookup('service:poll-settings'); |
| 66 | + const poll = generateMockPoll({ timezone }); |
| 67 | + const settings = service.getSettings(poll); |
| 68 | + |
| 69 | + assert.false(settings.mustChooseTimeZone); |
| 70 | + }); |
| 71 | + |
| 72 | + test("true if poll's time zone differs user's time zone", function (assert) { |
| 73 | + const timezone = |
| 74 | + Intl.DateTimeFormat().resolvedOptions().timeZone === 'America/Caracas' |
| 75 | + ? 'Europe/Berlin' |
| 76 | + : 'America/Caracas'; |
| 77 | + |
| 78 | + const service = this.owner.lookup('service:poll-settings'); |
| 79 | + const poll = generateMockPoll({ timezone }); |
| 80 | + const settings = service.getSettings(poll); |
| 81 | + |
| 82 | + assert.true(settings.mustChooseTimeZone); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + module('shouldUseLocalTimeZone', function () { |
| 87 | + test('defaults to null', function (assert) { |
| 88 | + const service = this.owner.lookup('service:poll-settings'); |
| 89 | + const poll = generateMockPoll(); |
| 90 | + const settings = service.getSettings(poll); |
| 91 | + |
| 92 | + assert.strictEqual(settings.shouldUseLocalTimeZone, null); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + module('timeZone', function () { |
| 97 | + test('is undefined if local time zone should be used', function (assert) { |
| 98 | + const service = this.owner.lookup('service:poll-settings'); |
| 99 | + const poll = generateMockPoll(); |
| 100 | + const settings = service.getSettings(poll); |
| 101 | + settings.shouldUseLocalTimeZone = true; |
| 102 | + |
| 103 | + assert.strictEqual(settings.timeZone, undefined); |
| 104 | + }); |
| 105 | + |
| 106 | + test("is undefined if poll's time zone is null", function (assert) { |
| 107 | + const service = this.owner.lookup('service:poll-settings'); |
| 108 | + const poll = generateMockPoll({ timezone: null }); |
| 109 | + const settings = service.getSettings(poll); |
| 110 | + |
| 111 | + assert.strictEqual(settings.timeZone, undefined); |
| 112 | + |
| 113 | + settings.shouldUseLocalTimeZone = false; |
| 114 | + assert.strictEqual(settings.timeZone, undefined); |
| 115 | + }); |
| 116 | + |
| 117 | + test("is poll's time zone if shouldn't use local time zone", function (assert) { |
| 118 | + const service = this.owner.lookup('service:poll-settings'); |
| 119 | + const poll = generateMockPoll({ timezone: 'Europe/Berlin' }); |
| 120 | + const settings = service.getSettings(poll); |
| 121 | + settings.shouldUseLocalTimeZone = false; |
| 122 | + |
| 123 | + assert.strictEqual(settings.timeZone, 'Europe/Berlin'); |
| 124 | + }); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments