|
1 | 1 | import { Backup } from "../src/Backup";
|
2 | 2 | import joplin from "api";
|
3 | 3 | import { when } from "jest-when";
|
| 4 | +import { I18n } from "i18n"; |
4 | 5 |
|
5 | 6 | let backup = null;
|
6 | 7 |
|
| 8 | +let spyOnLogVerbose = null; |
| 9 | +let spyOnLogInfo = null; |
| 10 | +let spyOnLogWarn = null; |
| 11 | +let spyOnLogError = null; |
| 12 | +let spyOnShowError = null; |
| 13 | + |
7 | 14 | describe("Password", function () {
|
8 | 15 | beforeEach(async () => {
|
9 | 16 | backup = new Backup() as any;
|
| 17 | + |
| 18 | + spyOnLogVerbose = jest |
| 19 | + .spyOn(backup.log, "verbose") |
| 20 | + .mockImplementation(() => {}); |
| 21 | + spyOnLogInfo = jest.spyOn(backup.log, "info").mockImplementation(() => {}); |
| 22 | + spyOnLogWarn = jest.spyOn(backup.log, "warn").mockImplementation(() => {}); |
| 23 | + spyOnLogError = jest |
| 24 | + .spyOn(backup.log, "error") |
| 25 | + .mockImplementation(() => {}); |
| 26 | + |
| 27 | + spyOnShowError = jest |
| 28 | + .spyOn(backup, "showError") |
| 29 | + .mockImplementation(() => {}); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(async () => { |
| 33 | + spyOnLogVerbose.mockReset(); |
| 34 | + spyOnLogInfo.mockReset(); |
| 35 | + spyOnLogWarn.mockReset(); |
| 36 | + spyOnLogError.mockReset(); |
| 37 | + spyOnShowError.mockReset(); |
10 | 38 | });
|
11 | 39 |
|
12 | 40 | it(`Check`, async () => {
|
@@ -75,8 +103,61 @@ describe("Password", function () {
|
75 | 103 | expect(await backup.checkPassword()).toBe(testCase.expected);
|
76 | 104 |
|
77 | 105 | await backup.enablePassword();
|
| 106 | + |
| 107 | + if (testCase.expected == 1) { |
| 108 | + expect(backup.password).toBe(testCase.password); |
| 109 | + } |
78 | 110 | expect(spyOnsSettingsSetValue).toBeCalledTimes(testCase.called);
|
| 111 | + expect(backup.log.error).toHaveBeenCalledTimes(0); |
| 112 | + expect(backup.log.warn).toHaveBeenCalledTimes(0); |
| 113 | + spyOnsSettingsSetValue.mockReset(); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + it(`Check node-7z bug`, async () => { |
| 118 | + const spyOnsSettingsValue = jest.spyOn(joplin.settings, "value"); |
| 119 | + const spyOnsSettingsSetValue = jest.spyOn(joplin.settings, "setValue"); |
| 120 | + jest.spyOn(backup, "getTranslation").mockImplementation(() => {}); |
| 121 | + const spyOnShowMsg = jest |
| 122 | + .spyOn(backup, "showMsg") |
| 123 | + .mockImplementation(() => {}); |
| 124 | + |
| 125 | + const testCases = [ |
| 126 | + { |
| 127 | + password: "1password", |
| 128 | + fail: false, |
| 129 | + }, |
| 130 | + { |
| 131 | + password: '2pass"word', |
| 132 | + fail: true, |
| 133 | + }, |
| 134 | + ]; |
| 135 | + |
| 136 | + for (const testCase of testCases) { |
| 137 | + when(spyOnsSettingsValue) |
| 138 | + .mockImplementation(() => Promise.resolve("no mockImplementation")) |
| 139 | + .calledWith("usePassword") |
| 140 | + .mockImplementation(() => Promise.resolve(true)) |
| 141 | + .calledWith("password") |
| 142 | + .mockImplementation(() => Promise.resolve(testCase.password)) |
| 143 | + .calledWith("passwordRepeat") |
| 144 | + .mockImplementation(() => Promise.resolve(testCase.password)); |
| 145 | + |
| 146 | + await backup.enablePassword(); |
| 147 | + |
| 148 | + if (testCase.fail == false) { |
| 149 | + expect(backup.password).toBe(testCase.password); |
| 150 | + expect(backup.log.error).toHaveBeenCalledTimes(0); |
| 151 | + expect(spyOnShowMsg).toHaveBeenCalledTimes(0); |
| 152 | + } else { |
| 153 | + expect(backup.password).toBe(null); |
| 154 | + expect(backup.log.error).toHaveBeenCalledTimes(1); |
| 155 | + expect(spyOnShowMsg).toHaveBeenCalledTimes(1); |
| 156 | + } |
| 157 | + |
| 158 | + expect(backup.log.warn).toHaveBeenCalledTimes(0); |
79 | 159 | spyOnsSettingsSetValue.mockReset();
|
| 160 | + spyOnShowMsg.mockReset(); |
80 | 161 | }
|
81 | 162 | });
|
82 | 163 | });
|
0 commit comments