Skip to content

Commit cfe0cbb

Browse files
committed
adding tests
1 parent c1b4e27 commit cfe0cbb

File tree

3 files changed

+116
-4
lines changed

3 files changed

+116
-4
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import chalk from 'chalk'
2+
import { switchOpen } from '../../../modules/featureFlag/featureFlagDecider'
3+
4+
jest.mock('open')
5+
jest.mock('opn')
6+
jest.mock('../../../api/clients/IOClients/apps/ToolbeltConfig')
7+
jest.mock('../../../api/logger')
8+
jest.mock('../../../api/error/ErrorReport')
9+
import open from 'open'
10+
import opn from 'opn'
11+
import { GlobalConfig, ToolbeltConfig, VersionCheckRes } from '../../../api/clients/IOClients/apps/ToolbeltConfig'
12+
import logger from '../../../api/logger'
13+
import { IOClientFactory } from '../../../api/clients/IOClients/IOClientFactory'
14+
import { ErrorReport, LogToUserOptions, CustomErrorReportBaseConstructorArgs } from '../../../api/error/ErrorReport'
15+
16+
const getGlobalConfigMock = jest.fn()
17+
const logErrorForUserMock = jest.fn()
18+
19+
class ToolbeltConfigMock extends ToolbeltConfig {
20+
public async versionValidate(_: string) {
21+
return { minVersion: '', validVersion: true, message: '' } as VersionCheckRes
22+
}
23+
24+
public async getGlobalConfig(): Promise<GlobalConfig> {
25+
return getGlobalConfigMock()
26+
}
27+
}
28+
29+
class ErrorReportMock extends ErrorReport {
30+
public logErrorForUser(_?: LogToUserOptions) {
31+
logErrorForUserMock()
32+
return this
33+
}
34+
}
35+
36+
describe('featureFlagDecider', () => {
37+
beforeAll(() => {
38+
const clientMock = new ToolbeltConfigMock(IOClientFactory.createIOContext(), {})
39+
jest.spyOn(ToolbeltConfig, 'createClient').mockReturnValue(clientMock)
40+
41+
const errorMock = new ErrorReportMock({ shouldRemoteReport: false } as CustomErrorReportBaseConstructorArgs)
42+
jest.spyOn(ErrorReport, 'createAndMaybeRegisterOnTelemetry').mockReturnValue(errorMock)
43+
})
44+
45+
beforeEach(() => {
46+
getGlobalConfigMock.mockClear()
47+
logErrorForUserMock.mockClear()
48+
})
49+
50+
describe('switchOpen', () => {
51+
let url: string
52+
let options: object
53+
54+
beforeAll(() => {
55+
url = 'https://vtex.myvtex.com'
56+
options = {}
57+
})
58+
59+
test('should log properly', async () => {
60+
getGlobalConfigMock.mockReturnValue({
61+
featureFlags: {
62+
FEATURE_FLAG_NEW_OPEN_PACKAGE: true,
63+
},
64+
})
65+
66+
await switchOpen(url, options)
67+
68+
expect(getGlobalConfigMock).toHaveBeenCalled()
69+
expect(logger.info).toHaveBeenCalledTimes(2)
70+
expect(logger.info).lastCalledWith(`${chalk.cyan(url)}`)
71+
})
72+
73+
test('should use open when FEATURE_FLAG_NEW_OPEN_PACKAGE is true', async () => {
74+
getGlobalConfigMock.mockReturnValue({
75+
featureFlags: {
76+
FEATURE_FLAG_NEW_OPEN_PACKAGE: true,
77+
},
78+
})
79+
80+
await switchOpen(url, options)
81+
82+
expect(getGlobalConfigMock).toHaveBeenCalled()
83+
expect(open).toHaveBeenCalled()
84+
})
85+
86+
test('should use opn when FEATURE_FLAG_NEW_OPEN_PACKAGE is false', async () => {
87+
getGlobalConfigMock.mockReturnValue({
88+
featureFlags: {
89+
FEATURE_FLAG_NEW_OPEN_PACKAGE: false,
90+
},
91+
})
92+
93+
await switchOpen(url, options)
94+
95+
expect(getGlobalConfigMock).toHaveBeenCalled()
96+
expect(opn).toHaveBeenCalled()
97+
})
98+
99+
test('should show a debug log in case of errors', async () => {
100+
const errorMsg = 'fake error'
101+
getGlobalConfigMock.mockImplementation(() => {
102+
throw new Error(errorMsg)
103+
})
104+
105+
await switchOpen(url, options)
106+
107+
expect(getGlobalConfigMock).toHaveBeenCalled()
108+
expect(ErrorReport.createAndMaybeRegisterOnTelemetry).toHaveBeenCalled()
109+
expect(ErrorReport)
110+
})
111+
})
112+
})

src/api/clients/IOClients/apps/ToolbeltConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { InstanceOptions, IOClient, IOContext } from '@vtex/api'
22
import { NodeToRender } from '@vtex/toolbelt-message-renderer'
33
import { IOClientFactory } from '../IOClientFactory'
44

5-
interface VersionCheckRes {
5+
export interface VersionCheckRes {
66
minVersion: string
77
validVersion: boolean
88
message: string
99
}
1010

11-
interface GlobalConfig {
11+
export interface GlobalConfig {
1212
config: Record<string, any>
1313
messages: Record<string, NodeToRender>
1414
featureFlags: Record<string, any>

src/api/error/ErrorReport.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface CustomErrorReportCreateArgs extends ErrorReportCreateArgs {
1717
shouldRemoteReport?: boolean
1818
}
1919

20-
interface CustomErrorReportBaseConstructorArgs extends ErrorReportBaseConstructorArgs {
20+
export interface CustomErrorReportBaseConstructorArgs extends ErrorReportBaseConstructorArgs {
2121
shouldRemoteReport: boolean
2222
}
2323

@@ -31,7 +31,7 @@ interface ErrorEnv {
3131
}
3232

3333
type ErrorLogLevel = 'error' | 'debug'
34-
interface LogToUserOptions {
34+
export interface LogToUserOptions {
3535
coreLogLevelDefault?: ErrorLogLevel
3636
requestDataLogLevelDefault?: ErrorLogLevel
3737
logLevels?: {

0 commit comments

Comments
 (0)