Skip to content

Commit d49533f

Browse files
clean up mocks
1 parent 6de304e commit d49533f

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

packages/server/test/unit/browsers/chrome_spec.js

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ const openOpts = {
1717
onError: () => {},
1818
}
1919

20+
// Helper function to create consistent mock preferences for testing
21+
const createMockDefaultPreferences = () => ({
22+
default: {
23+
fake_preference: {
24+
value: 'value',
25+
},
26+
},
27+
defaultSecure: {},
28+
localState: {
29+
fake_local_state: {
30+
value: 'value',
31+
},
32+
},
33+
})
34+
35+
// Helper function to mock _getDefaultChromePreferences with consistent fake preferences
36+
const mockGetDefaultChromePreferences = () => {
37+
return sinon.stub(chrome, '_getDefaultChromePreferences').returns(createMockDefaultPreferences())
38+
}
39+
2040
describe('lib/browsers/chrome', () => {
2141
context('#open', () => {
2242
beforeEach(function () {
@@ -963,6 +983,9 @@ describe('lib/browsers/chrome', () => {
963983
const securePrefs = outputJson.withArgs('/foo/Default/Secure Preferences').resolves()
964984
const statePrefs = outputJson.withArgs('/foo/Local State').resolves()
965985

986+
// Mock _getDefaultChromePreferences to return fake preferences for testing
987+
const mockDefaultPrefs = mockGetDefaultChromePreferences()
988+
966989
// Simulate empty preferences read from disk (no defaults exist yet)
967990
const originalPrefs = {
968991
default: {},
@@ -977,22 +1000,23 @@ describe('lib/browsers/chrome', () => {
9771000
.then(() => {
9781001
// Should write default preferences since they don't exist on disk
9791002
expect(defaultPrefs).to.be.calledWith('/foo/Default/Preferences', {
980-
autofill: {
981-
profile_enabled: false,
982-
credit_card_enabled: false,
1003+
fake_preference: {
1004+
value: 'value',
9831005
},
9841006
})
9851007

9861008
// defaultSecure is empty, so it should not be written
9871009
expect(securePrefs).to.not.be.called
9881010

9891011
expect(statePrefs).to.be.calledWith('/foo/Local State', {
990-
browser: {
991-
command_line_flag_security_warnings_enabled: false,
992-
promotions_enabled: false,
1012+
fake_local_state: {
1013+
value: 'value',
9931014
},
9941015
})
9951016
})
1017+
.finally(() => {
1018+
mockDefaultPrefs.restore()
1019+
})
9961020
})
9971021
})
9981022

@@ -1017,25 +1041,8 @@ describe('lib/browsers/chrome', () => {
10171041
})
10181042

10191043
it('merges defaults with existing preferences', () => {
1020-
const mockDefaults = {
1021-
default: {
1022-
fake_preference: {
1023-
value: 'value',
1024-
},
1025-
},
1026-
defaultSecure: {
1027-
fake_secure_preference: {
1028-
value: 'value',
1029-
},
1030-
},
1031-
localState: {
1032-
fake_local_state: {
1033-
value: 'value',
1034-
},
1035-
},
1036-
}
1037-
1038-
sinon.stub(chrome, '_getDefaultChromePreferences').returns(mockDefaults)
1044+
const mockDefaults = createMockDefaultPreferences()
1045+
const mockDefaultPrefs = sinon.stub(chrome, '_getDefaultChromePreferences').returns(mockDefaults)
10391046

10401047
fs.readJson.withArgs('/foo/Default/Preferences').resolves({ existing: 'value' })
10411048
fs.readJson.withArgs('/foo/Default/Secure Preferences').resolves({ secure: 'value' })
@@ -1046,18 +1053,13 @@ describe('lib/browsers/chrome', () => {
10461053
expect(result).to.deep.eq(mockDefaults)
10471054
})
10481055
.finally(() => {
1049-
chrome._getDefaultChromePreferences.restore()
1056+
mockDefaultPrefs.restore()
10501057
})
10511058
})
10521059

10531060
it('returns defaults when no existing preferences', () => {
1054-
const mockDefaults = {
1055-
default: { test: 'default' },
1056-
defaultSecure: { secure: 'default' },
1057-
localState: { local: 'default' },
1058-
}
1059-
1060-
sinon.stub(chrome, '_getDefaultChromePreferences').returns(mockDefaults)
1061+
const mockDefaults = createMockDefaultPreferences()
1062+
const mockDefaultPrefs = sinon.stub(chrome, '_getDefaultChromePreferences').returns(mockDefaults)
10611063

10621064
fs.readJson.withArgs('/foo/Default/Preferences').rejects({ code: 'ENOENT' })
10631065
fs.readJson.withArgs('/foo/Default/Secure Preferences').rejects({ code: 'ENOENT' })
@@ -1068,7 +1070,7 @@ describe('lib/browsers/chrome', () => {
10681070
expect(result).to.deep.eq(mockDefaults)
10691071
})
10701072
.finally(() => {
1071-
chrome._getDefaultChromePreferences.restore()
1073+
mockDefaultPrefs.restore()
10721074
})
10731075
})
10741076
})
@@ -1118,6 +1120,9 @@ describe('lib/browsers/chrome', () => {
11181120

11191121
context('#_mergeChromePreferences with user preferences', () => {
11201122
it('merges user preferences with defaults correctly', () => {
1123+
// Mock _getDefaultChromePreferences to return fake preferences for testing
1124+
const mockDefaultPrefs = mockGetDefaultChromePreferences()
1125+
11211126
const defaultPrefs = chrome._getDefaultChromePreferences()
11221127
const userPrefs = {
11231128
default: {
@@ -1154,6 +1159,8 @@ describe('lib/browsers/chrome', () => {
11541159
},
11551160
newLocalSetting: 'userValue', // User addition
11561161
})
1162+
1163+
mockDefaultPrefs.restore()
11571164
})
11581165

11591166
it('handles preference deletion with null values', () => {
@@ -1281,6 +1288,9 @@ describe('lib/browsers/chrome', () => {
12811288
sinon.stub(protocol, 'getRemoteDebuggingPort').resolves(50505)
12821289
sinon.stub(launch, 'launch').resolves(this.launchedBrowser)
12831290

1291+
// Mock _getDefaultChromePreferences to return fake preferences for testing
1292+
this.mockDefaultPrefs = mockGetDefaultChromePreferences()
1293+
12841294
this.readJson = sinon.stub(fs, 'readJson')
12851295
this.readJson.withArgs('/profile/dir/Default/Preferences').rejects({ code: 'ENOENT' })
12861296
this.readJson.withArgs('/profile/dir/Default/Secure Preferences').rejects({ code: 'ENOENT' })
@@ -1295,6 +1305,7 @@ describe('lib/browsers/chrome', () => {
12951305
protocol.getRemoteDebuggingPort.restore()
12961306
fs.readJson.restore()
12971307
fs.outputJson.restore()
1308+
this.mockDefaultPrefs.restore()
12981309
})
12991310

13001311
it('writes default preferences during browser launch', async function () {
@@ -1356,10 +1367,8 @@ describe('lib/browsers/chrome', () => {
13561367
expect(this.outputJson).to.have.been.calledWith(
13571368
'/profile/dir/Local State',
13581369
sinon.match({
1359-
browser: {
1360-
fake_local_state: {
1361-
value: 'value',
1362-
},
1370+
fake_local_state: {
1371+
value: 'value',
13631372
},
13641373
}),
13651374
)

0 commit comments

Comments
 (0)