Repository: n8n-io/n8n
File: packages/frontend/editor-ui/src/features/integrations/secretsProviders.ee/composables/useSecretsProvidersList.ee.test.ts (TypeScript)

Code before:

		it('should update existing connection in activeConnections', async () => {
			vi.spyOn(rbacStore, 'hasScope').mockReturnValue(true);
			vi.mocked(secretsProviderApi.getSecretProviderConnections).mockResolvedValue(mockConnections);

			const updatedConnection: SecretProviderConnection = {
				...mockConnections[0],
				secretsCount: 10,
				state: 'connected',
			};
			vi.mocked(secretsProviderApi.getSecretProviderConnectionByKey).mockResolvedValue(
				updatedConnection,
			);

			const { fetchActiveConnections, fetchConnection, activeProviders } =
				useSecretsProvidersList();

			await fetchActiveConnections();
			expect(activeProviders.value.find((p) => p.name === 'aws-prod')?.secretsCount).toBe(5);

			await fetchConnection('aws-prod');
			expect(secretsProviderApi.getSecretProviderConnectionByKey).toHaveBeenCalledTimes(1);
			expect(activeProviders.value.find((p) => p.name === 'aws-prod')?.secretsCount).toBe(10);
		});

		it('should not modify activeConnections when provider key is not found', async () => {
			vi.spyOn(rbacStore, 'hasScope').mockReturnValue(true);
			vi.mocked(secretsProviderApi.getSecretProviderConnections).mockResolvedValue(mockConnections);

			const newConnection: SecretProviderConnection = {
				id: '3',
				name: 'unknown-provider',
				type: 'vault',
				state: 'connected',
				isEnabled: true,
				projects: [],
				settings: {},
				secretsCount: 1,
				secrets: [],
				createdAt: '2024-01-25T10:00:00Z',
				updatedAt: '2024-01-25T10:00:00Z',
			};
			vi.mocked(secretsProviderApi.getSecretProviderConnectionByKey).mockResolvedValue(
				newConnection,
			);

			const { fetchActiveConnections, fetchConnection, activeProviders } =
				useSecretsProvidersList();

			await fetchActiveConnections();
			expect(activeProviders.value).toHaveLength(2);

Review comment (suggestion):
Just thinking out loud: checking for the length of "activeProviders.value" seems like a bit too lenient of a logic to match the "should not modify" claim of this test name.

Code after:

		it('should update existing connection in activeConnections', async () => {
			vi.spyOn(rbacStore, 'hasScope').mockReturnValue(true);
			vi.mocked(secretsProviderApi.getSecretProviderConnections).mockResolvedValue(mockConnections);

			const updatedConnection: SecretProviderConnection = {
				...mockConnections[0],
				secretsCount: 10,
				state: 'connected',
			};
			vi.mocked(secretsProviderApi.getSecretProviderConnectionByKey).mockResolvedValue(
				updatedConnection,
			);

			const { fetchActiveConnections, fetchConnection, activeProviders } =
				useSecretsProvidersList();

			await fetchActiveConnections();
			expect(activeProviders.value.find((p) => p.name === 'aws-prod')?.secretsCount).toBe(5);

			await fetchConnection('aws-prod');
			expect(secretsProviderApi.getSecretProviderConnectionByKey).toHaveBeenCalledTimes(1);
			expect(activeProviders.value.find((p) => p.name === 'aws-prod')?.secretsCount).toBe(10);
		});

		it('should not modify activeConnections when provider key is not found', async () => {
			vi.spyOn(rbacStore, 'hasScope').mockReturnValue(true);
			vi.mocked(secretsProviderApi.getSecretProviderConnections).mockResolvedValue(mockConnections);

			const newConnection: SecretProviderConnection = {
				id: '3',
				name: 'unknown-provider',
				type: 'vault',
				state: 'connected',
				isEnabled: true,
				projects: [],
				settings: {},
				secretsCount: 1,
				secrets: [],
				createdAt: '2024-01-25T10:00:00Z',
				updatedAt: '2024-01-25T10:00:00Z',
			};
			vi.mocked(secretsProviderApi.getSecretProviderConnectionByKey).mockResolvedValue(
				newConnection,
			);

			const { fetchActiveConnections, fetchConnection, activeProviders } =
				useSecretsProvidersList();

			await fetchActiveConnections();
			const providersBefore = activeProviders.value.map((p) => ({ ...p }));