|
| 1 | +import { firstValueFrom } from "rxjs"; |
| 2 | + |
| 3 | +import { Utils } from "@bitwarden/common/platform/misc/utils"; |
| 4 | +import { UserId } from "@bitwarden/common/types/guid"; |
| 5 | + |
| 6 | +import { |
| 7 | + FakeAccountService, |
| 8 | + FakeSingleUserState, |
| 9 | + FakeStateProvider, |
| 10 | + mockAccountServiceWith, |
| 11 | +} from "../../../common/spec"; |
| 12 | + |
| 13 | +import { |
| 14 | + NewDeviceVerificationNoticeService, |
| 15 | + NewDeviceVerificationNotice, |
| 16 | + NEW_DEVICE_VERIFICATION_NOTICE_KEY, |
| 17 | +} from "./new-device-verification-notice.service"; |
| 18 | + |
| 19 | +describe("New Device Verification Notice", () => { |
| 20 | + const sut = NEW_DEVICE_VERIFICATION_NOTICE_KEY; |
| 21 | + const userId = Utils.newGuid() as UserId; |
| 22 | + let newDeviceVerificationService: NewDeviceVerificationNoticeService; |
| 23 | + let mockNoticeState: FakeSingleUserState<NewDeviceVerificationNotice>; |
| 24 | + let stateProvider: FakeStateProvider; |
| 25 | + let accountService: FakeAccountService; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + accountService = mockAccountServiceWith(userId); |
| 29 | + stateProvider = new FakeStateProvider(accountService); |
| 30 | + mockNoticeState = stateProvider.singleUser.getFake(userId, NEW_DEVICE_VERIFICATION_NOTICE_KEY); |
| 31 | + newDeviceVerificationService = new NewDeviceVerificationNoticeService(stateProvider); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should deserialize newDeviceVerificationNotice values", async () => { |
| 35 | + const currentDate = new Date(); |
| 36 | + const inputObj = { |
| 37 | + last_dismissal: currentDate, |
| 38 | + permanent_dismissal: false, |
| 39 | + }; |
| 40 | + |
| 41 | + const expectedFolderData = { |
| 42 | + last_dismissal: currentDate.toJSON(), |
| 43 | + permanent_dismissal: false, |
| 44 | + }; |
| 45 | + |
| 46 | + const result = sut.deserializer(JSON.parse(JSON.stringify(inputObj))); |
| 47 | + |
| 48 | + expect(result).toEqual(expectedFolderData); |
| 49 | + }); |
| 50 | + |
| 51 | + describe("notice$", () => { |
| 52 | + it("emits new device verification notice state", async () => { |
| 53 | + const currentDate = new Date(); |
| 54 | + const data = { |
| 55 | + last_dismissal: currentDate, |
| 56 | + permanent_dismissal: false, |
| 57 | + }; |
| 58 | + await stateProvider.setUserState(NEW_DEVICE_VERIFICATION_NOTICE_KEY, data, userId); |
| 59 | + |
| 60 | + const result = await firstValueFrom(newDeviceVerificationService.noticeState$(userId)); |
| 61 | + |
| 62 | + expect(result).toBe(data); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("update notice state", () => { |
| 67 | + it("should update the date with a new value", async () => { |
| 68 | + const currentDate = new Date(); |
| 69 | + const oldDate = new Date("11-11-2011"); |
| 70 | + const oldState = { |
| 71 | + last_dismissal: oldDate, |
| 72 | + permanent_dismissal: false, |
| 73 | + }; |
| 74 | + const newState = { |
| 75 | + last_dismissal: currentDate, |
| 76 | + permanent_dismissal: true, |
| 77 | + }; |
| 78 | + mockNoticeState.nextState(oldState); |
| 79 | + await newDeviceVerificationService.updateNewDeviceVerificationNoticeState(userId, newState); |
| 80 | + |
| 81 | + const result = await firstValueFrom(newDeviceVerificationService.noticeState$(userId)); |
| 82 | + expect(result).toEqual(newState); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments