fix(frontend-plugin-api): mock analytics context type

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2023-12-09 12:23:24 +01:00
parent 3bb938d742
commit d653c139e6
5 changed files with 135 additions and 3 deletions
+9 -2
View File
@@ -5,10 +5,11 @@
```ts
/// <reference types="react" />
import { AnalyticsApi } from '@backstage/frontend-plugin-api';
import { AnalyticsEvent } from '@backstage/frontend-plugin-api';
import { ErrorWithContext } from '@backstage/test-utils';
import { ExtensionDefinition } from '@backstage/frontend-plugin-api';
import { JsonObject } from '@backstage/types';
import { MockAnalyticsApi } from '@backstage/test-utils';
import { MockConfigApi } from '@backstage/test-utils';
import { MockErrorApi } from '@backstage/test-utils';
import { MockErrorApiOptions } from '@backstage/test-utils';
@@ -47,7 +48,13 @@ export class ExtensionTester {
render(options?: { config?: JsonObject }): RenderResult;
}
export { MockAnalyticsApi };
// @public
export class MockAnalyticsApi implements AnalyticsApi {
// (undocumented)
captureEvent(event: AnalyticsEvent): void;
// (undocumented)
getEvents(): AnalyticsEvent[];
}
export { MockConfigApi };
@@ -0,0 +1,64 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { MockAnalyticsApi } from './MockAnalyticsApi';
describe('MockAnalyticsApi', () => {
const context = {
pluginId: 'some-plugin',
extensionId: 'some-extension',
};
it('should collect events', () => {
const api = new MockAnalyticsApi();
api.captureEvent({ action: 'action-1', subject: 'subject-1', context });
api.captureEvent({
action: 'action-2',
subject: 'subject-2',
value: 42,
context,
});
api.captureEvent({
action: 'action-3',
subject: 'subject-3',
value: 1337,
attributes: { some: 'context' },
context,
});
expect(api.getEvents()[0]).toMatchObject({
subject: 'subject-1',
action: 'action-1',
context,
});
expect(api.getEvents()[1]).toMatchObject({
subject: 'subject-2',
action: 'action-2',
value: 42,
context,
});
expect(api.getEvents()[2]).toMatchObject({
subject: 'subject-3',
action: 'action-3',
value: 1337,
context,
attributes: {
some: 'context',
},
});
});
});
@@ -0,0 +1,43 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AnalyticsApi, AnalyticsEvent } from '@backstage/frontend-plugin-api';
/**
* Mock implementation of {@link frontend-plugin-api#AnalyticsApi} with helpers to ensure that events are sent correctly.
* Use getEvents in tests to verify captured events.
*
* @public
*/
export class MockAnalyticsApi implements AnalyticsApi {
private events: AnalyticsEvent[] = [];
captureEvent(event: AnalyticsEvent) {
const { action, subject, value, attributes, context } = event;
this.events.push({
action,
subject,
context,
...(value !== undefined ? { value } : {}),
...(attributes !== undefined ? { attributes } : {}),
});
}
getEvents(): AnalyticsEvent[] {
return this.events;
}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { MockAnalyticsApi } from './MockAnalyticsApi';
@@ -15,7 +15,6 @@
*/
export {
MockAnalyticsApi,
MockConfigApi,
type ErrorWithContext,
MockErrorApi,
@@ -26,3 +25,5 @@ export {
MockStorageApi,
type MockStorageBucket,
} from '@backstage/test-utils';
export { MockAnalyticsApi } from './AnalyticsApi/MockAnalyticsApi';