|
1 |
| -import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 1 | +import { describe, expect, it } from 'vitest'; |
2 | 2 | import { Utility } from '../../src/index.js';
|
3 | 3 |
|
4 | 4 | describe('Class: Utility', () => {
|
5 |
| - beforeEach(() => { |
6 |
| - vi.clearAllMocks(); |
7 |
| - vi.resetModules(); |
8 |
| - }); |
| 5 | + class TestUtility extends Utility { |
| 6 | + public dummyMethod(): boolean { |
| 7 | + return this.getColdStart(); |
| 8 | + } |
| 9 | + public isColdStart(): boolean { |
| 10 | + return this.coldStart; |
| 11 | + } |
| 12 | + public getServiceName(): string { |
| 13 | + return this.defaultServiceName; |
| 14 | + } |
| 15 | + public validateServiceName(serviceName: string): boolean { |
| 16 | + return this.isValidServiceName(serviceName); |
| 17 | + } |
| 18 | + } |
9 | 19 |
|
10 |
| - describe('Method: getDefaultServiceName', () => { |
11 |
| - it('returns the default service name', () => { |
12 |
| - class PowerTool extends Utility { |
13 |
| - public dummyMethod(): string { |
14 |
| - return this.getDefaultServiceName(); |
15 |
| - } |
16 |
| - } |
| 20 | + it('returns the correct cold start value', () => { |
| 21 | + // Prepare |
| 22 | + const utility = new TestUtility(); |
17 | 23 |
|
18 |
| - const powertool = new PowerTool(); |
19 |
| - const result = powertool.dummyMethod(); |
20 |
| - expect(result).toBe('service_undefined'); |
21 |
| - }); |
| 24 | + // Act & Assess |
| 25 | + expect(utility.dummyMethod()).toBe(true); |
| 26 | + expect(utility.dummyMethod()).toBe(false); |
| 27 | + expect(utility.dummyMethod()).toBe(false); |
22 | 28 | });
|
23 | 29 |
|
24 |
| - describe('Method: getColdStart', () => { |
25 |
| - it('it returns true the first time, then false afterwards, when called multiple times', () => { |
26 |
| - // Prepare |
27 |
| - const utility = new Utility(); |
28 |
| - const getColdStartSpy = vi.spyOn(utility, 'getColdStart'); |
29 |
| - |
30 |
| - // Act |
31 |
| - utility.getColdStart(); |
32 |
| - utility.getColdStart(); |
33 |
| - utility.getColdStart(); |
34 |
| - utility.getColdStart(); |
35 |
| - utility.getColdStart(); |
36 |
| - |
37 |
| - // Assess |
38 |
| - expect(getColdStartSpy).toHaveBeenCalledTimes(5); |
39 |
| - expect(getColdStartSpy.mock.results).toEqual([ |
40 |
| - expect.objectContaining({ value: true }), |
41 |
| - expect.objectContaining({ value: false }), |
42 |
| - expect.objectContaining({ value: false }), |
43 |
| - expect.objectContaining({ value: false }), |
44 |
| - expect.objectContaining({ value: false }), |
45 |
| - ]); |
46 |
| - }); |
47 |
| - |
48 |
| - it('returns the correct values when subclassed', () => { |
49 |
| - // Prepare |
50 |
| - class PowerTool extends Utility { |
51 |
| - public dummyMethod(): boolean { |
52 |
| - return this.getColdStart(); |
53 |
| - } |
54 |
| - } |
55 |
| - const powertool = new PowerTool(); |
56 |
| - const dummyMethodSpy = vi.spyOn(powertool, 'dummyMethod'); |
57 |
| - const getColdStartSpy = vi.spyOn(powertool, 'getColdStart'); |
| 30 | + it('flips the cold start value', () => { |
| 31 | + // Prepare |
| 32 | + const utility = new TestUtility(); |
58 | 33 |
|
59 |
| - // Act |
60 |
| - powertool.dummyMethod(); |
61 |
| - powertool.dummyMethod(); |
62 |
| - powertool.dummyMethod(); |
63 |
| - powertool.dummyMethod(); |
64 |
| - powertool.dummyMethod(); |
| 34 | + // Act |
| 35 | + utility.dummyMethod(); |
65 | 36 |
|
66 |
| - // Assess |
67 |
| - expect(dummyMethodSpy).toHaveBeenCalledTimes(5); |
68 |
| - expect(getColdStartSpy).toHaveBeenCalledTimes(5); |
69 |
| - expect(dummyMethodSpy.mock.results).toEqual([ |
70 |
| - expect.objectContaining({ value: true }), |
71 |
| - expect.objectContaining({ value: false }), |
72 |
| - expect.objectContaining({ value: false }), |
73 |
| - expect.objectContaining({ value: false }), |
74 |
| - expect.objectContaining({ value: false }), |
75 |
| - ]); |
76 |
| - }); |
| 37 | + // Assess |
| 38 | + expect(utility.isColdStart()).toBe(false); |
77 | 39 | });
|
78 | 40 |
|
79 |
| - describe('Method: isColdStart', () => { |
80 |
| - it('returns true the first time, then false afterwards when called multiple times', () => { |
81 |
| - // Prepare |
82 |
| - const utility = new Utility(); |
83 |
| - const isColdStartSpy = vi.spyOn(utility, 'isColdStart'); |
| 41 | + it('returns the correct default service name', () => { |
| 42 | + // Prepare |
| 43 | + const utility = new TestUtility(); |
84 | 44 |
|
85 |
| - // Act |
86 |
| - utility.isColdStart(); |
87 |
| - utility.isColdStart(); |
88 |
| - utility.isColdStart(); |
89 |
| - utility.isColdStart(); |
90 |
| - utility.isColdStart(); |
91 |
| - |
92 |
| - // Assess |
93 |
| - expect(isColdStartSpy).toHaveBeenCalledTimes(5); |
94 |
| - expect(isColdStartSpy.mock.results).toEqual([ |
95 |
| - expect.objectContaining({ value: true }), |
96 |
| - expect.objectContaining({ value: false }), |
97 |
| - expect.objectContaining({ value: false }), |
98 |
| - expect.objectContaining({ value: false }), |
99 |
| - expect.objectContaining({ value: false }), |
100 |
| - ]); |
101 |
| - }); |
102 |
| - |
103 |
| - it('returns the correct values when subclassed', () => { |
104 |
| - // Prepare |
105 |
| - class PowerTool extends Utility { |
106 |
| - public dummyMethod(): boolean { |
107 |
| - return this.isColdStart(); |
108 |
| - } |
109 |
| - } |
110 |
| - const powertool = new PowerTool(); |
111 |
| - const dummyMethodSpy = vi.spyOn(powertool, 'dummyMethod'); |
112 |
| - const isColdStartSpy = vi.spyOn(powertool, 'isColdStart'); |
113 |
| - |
114 |
| - // Act |
115 |
| - powertool.dummyMethod(); |
116 |
| - powertool.dummyMethod(); |
117 |
| - powertool.dummyMethod(); |
118 |
| - powertool.dummyMethod(); |
119 |
| - powertool.dummyMethod(); |
120 |
| - |
121 |
| - // Assess |
122 |
| - expect(dummyMethodSpy).toHaveBeenCalledTimes(5); |
123 |
| - expect(isColdStartSpy).toHaveBeenCalledTimes(5); |
124 |
| - expect(dummyMethodSpy.mock.results).toEqual([ |
125 |
| - expect.objectContaining({ value: true }), |
126 |
| - expect.objectContaining({ value: false }), |
127 |
| - expect.objectContaining({ value: false }), |
128 |
| - expect.objectContaining({ value: false }), |
129 |
| - expect.objectContaining({ value: false }), |
130 |
| - ]); |
131 |
| - }); |
| 45 | + // Assess |
| 46 | + expect(utility.getServiceName()).toBe('service_undefined'); |
132 | 47 | });
|
133 | 48 |
|
134 |
| - describe('Method: isValidServiceName', () => { |
135 |
| - class PowerTool extends Utility { |
136 |
| - public dummyMethod(name: string): boolean { |
137 |
| - return this.isValidServiceName(name); |
138 |
| - } |
139 |
| - } |
140 |
| - it('allows valid strings', () => { |
141 |
| - const powertool = new PowerTool(); |
142 |
| - const goodName = 'serverlessAirline'; |
143 |
| - |
144 |
| - const result = powertool.dummyMethod(goodName); |
145 |
| - |
146 |
| - expect(result).toBe(true); |
147 |
| - }); |
148 |
| - |
149 |
| - it("doesn't allow empty strings", () => { |
150 |
| - const tooShort = ''; |
151 |
| - const powertool = new PowerTool(); |
152 |
| - const result = powertool.dummyMethod(tooShort); |
| 49 | + it('validates service name', () => { |
| 50 | + // Prepare |
| 51 | + const utility = new TestUtility(); |
153 | 52 |
|
154 |
| - expect(result).toBe(false); |
155 |
| - }); |
| 53 | + // Act & Assess |
| 54 | + expect(utility.validateServiceName('serverlessAirline')).toBe(true); |
| 55 | + expect(utility.validateServiceName('')).toBe(false); |
156 | 56 | });
|
157 | 57 | });
|
0 commit comments