Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 68 additions & 40 deletions packages/sdk/electron/__tests__/ElectronClient.mainProcess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,23 @@ jest.mock('../src/platform/ElectronPlatform', () => ({
const clientSideId = 'client-side-id';
const DEFAULT_INITIAL_CONTEXT = { kind: 'user' as const, key: 'test-user' };

// A client that stays open keeps its poll and flush timers, and jest cannot exit.
const openClients: { close(): Promise<void> }[] = [];

function closeAfterTest<T extends { close(): Promise<void> }>(client: T): T {
openClients.push(client);
return client;
}

beforeEach(() => {
jest.clearAllMocks();
handlers.clear();
});

afterEach(async () => {
await Promise.all(openClients.splice(0).map((client) => client.close()));
});

describe('given an initialized ElectronClient with enableIPC: false', () => {
const logger: LDLogger = {
debug: jest.fn(),
Expand All @@ -121,6 +133,10 @@ describe('given an initialized ElectronClient with enableIPC: false', () => {
await client.start();
});

afterAll(async () => {
await client.close();
});

it('does not register any IPC channels so ipcMain.on and ipcMain.handle are never called', () => {
expect(mockOn).not.toHaveBeenCalled();
expect(mockHandle).not.toHaveBeenCalled();
Expand Down Expand Up @@ -195,12 +211,14 @@ describe('given an initialized ElectronClient with enableIPC: false and bootstra
};

it('evaluates flags from bootstrap when called directly on the client', async () => {
const client = new ElectronClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'offline',
enableIPC: false,
logger,
diagnosticOptOut: true,
});
const client = closeAfterTest(
new ElectronClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'offline',
enableIPC: false,
logger,
diagnosticOptOut: true,
}),
);
await client.start({ bootstrap: goodBootstrapData });

expect(client.variation('killswitch', false)).toBe(true);
Expand All @@ -223,12 +241,14 @@ describe('enableIPC: false and close()', () => {
};

it('does not register channels so close() does not remove any handlers', async () => {
const client = new ElectronClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'offline',
enableIPC: false,
logger,
diagnosticOptOut: true,
});
const client = closeAfterTest(
new ElectronClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'offline',
enableIPC: false,
logger,
diagnosticOptOut: true,
}),
);
await client.start();

expect(mockOn).not.toHaveBeenCalled();
Expand Down Expand Up @@ -267,13 +287,15 @@ describe('given an initialized ElectronClient with enableIPC: false and polling'
},
});

const client = createClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'polling',
enableIPC: false,
logger,
diagnosticOptOut: true,
sendEvents: false,
});
const client = closeAfterTest(
createClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'polling',
enableIPC: false,
logger,
diagnosticOptOut: true,
sendEvents: false,
}),
);
await client.start();

expect(client.boolVariation('on-off-flag', false)).toBe(true);
Expand Down Expand Up @@ -319,13 +341,15 @@ describe('maxCachedContexts', () => {
storage,
});

const client = createClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'polling',
enableIPC: false,
diagnosticOptOut: true,
sendEvents: false,
maxCachedContexts: 1,
});
const client = closeAfterTest(
createClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'polling',
enableIPC: false,
diagnosticOptOut: true,
sendEvents: false,
maxCachedContexts: 1,
}),
);
await client.start();

// After start, context A's flags are cached.
Expand Down Expand Up @@ -364,13 +388,15 @@ describe('maxCachedContexts', () => {
storage,
});

const client = createClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'polling',
enableIPC: false,
diagnosticOptOut: true,
sendEvents: false,
maxCachedContexts: 0,
});
const client = closeAfterTest(
createClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'polling',
enableIPC: false,
diagnosticOptOut: true,
sendEvents: false,
maxCachedContexts: 0,
}),
);
await client.start();

// Flags should still evaluate correctly from the network response
Expand Down Expand Up @@ -412,13 +438,15 @@ describe('given an initialized ElectronClient with enableIPC: false and streamin
},
});

const client = createClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'streaming',
enableIPC: false,
logger,
diagnosticOptOut: true,
sendEvents: false,
});
const client = closeAfterTest(
createClient(clientSideId, DEFAULT_INITIAL_CONTEXT, {
initialConnectionMode: 'streaming',
enableIPC: false,
logger,
diagnosticOptOut: true,
sendEvents: false,
}),
);
await client.start();

expect(client.boolVariation('on-off-flag', false)).toBe(true);
Expand Down
Loading
Loading