|
| 1 | +/* |
| 2 | +Copyright 2024 API Testing Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { describe } from 'node:test' |
| 18 | +import {Cache, SetPreference, TestCaseResponse, Store, Stores} from '../cache' |
| 19 | + |
| 20 | +const localStorageMock = (() => { |
| 21 | + let store = {}; |
| 22 | + |
| 23 | + return { |
| 24 | + getItem(key) { |
| 25 | + return store[key] || null; |
| 26 | + }, |
| 27 | + setItem(key, value) { |
| 28 | + store[key] = value.toString(); |
| 29 | + }, |
| 30 | + removeItem(key) { |
| 31 | + delete store[key]; |
| 32 | + }, |
| 33 | + clear() { |
| 34 | + store = {}; |
| 35 | + } |
| 36 | + }; |
| 37 | +})(); |
| 38 | + |
| 39 | +Object.defineProperty(global, 'sessionStorage', { |
| 40 | + value: localStorageMock |
| 41 | +}); |
| 42 | +Object.defineProperty(global, 'localStorage', { |
| 43 | + value: localStorageMock |
| 44 | +}); |
| 45 | +Object.defineProperty(global, 'navigator', { |
| 46 | + value: { |
| 47 | + language: 'en' |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +describe('TestCaseResponseCache', () => { |
| 52 | + test('should set and get test case response cache', () => { |
| 53 | + const id = 'test-case-id' |
| 54 | + const resp = { |
| 55 | + output: 'test-body', |
| 56 | + body: {}, |
| 57 | + statusCode: 200, |
| 58 | + } as TestCaseResponse |
| 59 | + Cache.SetTestCaseResponseCache(id, resp) |
| 60 | + const result = Cache.GetTestCaseResponseCache(id) |
| 61 | + expect(result).toEqual(resp) |
| 62 | + }) |
| 63 | + |
| 64 | + test('get a non-existent test case response cache', () => { |
| 65 | + expect(Cache.GetTestCaseResponseCache('non-existent-id')).toEqual({}) |
| 66 | + }) |
| 67 | +}) |
| 68 | + |
| 69 | +describe('LastTestCaseLocation', () => { |
| 70 | + test('should get empty object when no last test case location', () => { |
| 71 | + expect(Cache.GetLastTestCaseLocation()).toEqual({}) |
| 72 | + }) |
| 73 | + |
| 74 | + test('should set and get last test case location', () => { |
| 75 | + const suite = 'test-suite' |
| 76 | + const testcase = 'test-case' |
| 77 | + Cache.SetLastTestCaseLocation(suite, testcase) |
| 78 | + const result = Cache.GetLastTestCaseLocation() |
| 79 | + expect(result).toEqual({ suite, testcase }) |
| 80 | + }) |
| 81 | +}) |
| 82 | + |
| 83 | +describe('Preference', () => { |
| 84 | + test('get the default preference', () => { |
| 85 | + expect(Cache.GetPreference()).toEqual({ |
| 86 | + darkTheme: false, |
| 87 | + requestActiveTab: 'body', |
| 88 | + responseActiveTab: 'body', |
| 89 | + language: 'en', |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + test('set and get preference', () => { |
| 94 | + const preference = { |
| 95 | + darkTheme: true, |
| 96 | + requestActiveTab: 'header', |
| 97 | + responseActiveTab: 'header', |
| 98 | + language: 'zh-cn', |
| 99 | + } |
| 100 | + SetPreference(preference) |
| 101 | + expect(Cache.GetPreference()).toEqual(preference) |
| 102 | + }) |
| 103 | + |
| 104 | + test('set and get dark theme', () => { |
| 105 | + Cache.WithDarkTheme(true) |
| 106 | + expect(Cache.GetPreference().darkTheme).toEqual(true) |
| 107 | + }) |
| 108 | + |
| 109 | + test('set and get request active tab', () => { |
| 110 | + Cache.WithRequestActiveTab('request') |
| 111 | + expect(Cache.GetPreference().requestActiveTab).toEqual('request') |
| 112 | + }) |
| 113 | + |
| 114 | + test('set and get response active tab', () => { |
| 115 | + Cache.WithResponseActiveTab('response') |
| 116 | + expect(Cache.GetPreference().responseActiveTab).toEqual('response') |
| 117 | + }) |
| 118 | + |
| 119 | + it('set and get language', () => { |
| 120 | + Cache.WithLocale('zh-cn') |
| 121 | + expect(Cache.GetPreference().language).toEqual('zh-cn') |
| 122 | + }) |
| 123 | +}) |
| 124 | + |
| 125 | +describe('stores', () => { |
| 126 | + test('should get empty object when no stores', () => { |
| 127 | + expect(Cache.GetCurrentStore()).toEqual({}) |
| 128 | + }) |
| 129 | + |
| 130 | + test('should set and get stores', () => { |
| 131 | + const stores = { |
| 132 | + current: 'test-store', |
| 133 | + items: [ |
| 134 | + { |
| 135 | + name: 'test-store', |
| 136 | + readOnly: false, |
| 137 | + } as Store, |
| 138 | + { |
| 139 | + name: 'read-only-store', |
| 140 | + readOnly: true, |
| 141 | + } as Store, |
| 142 | + ], |
| 143 | + } |
| 144 | + Cache.SetStores(stores) |
| 145 | + expect(Cache.GetCurrentStore()).toEqual({ |
| 146 | + name: 'test-store', |
| 147 | + readOnly: false, |
| 148 | + }) |
| 149 | + |
| 150 | + Cache.SetCurrentStore('read-only-store') |
| 151 | + expect(Cache.GetCurrentStore()).toEqual({ |
| 152 | + name: 'read-only-store', |
| 153 | + readOnly: true, |
| 154 | + }) |
| 155 | + |
| 156 | + Cache.SetStores({} as Stores) |
| 157 | + }) |
| 158 | +}) |
0 commit comments