npm package diff
Package: @forge/events
Versions: 2.0.1-next.0 - 2.0.1-next.1
File: package/src/__test__/appEvents.test.ts
Index: package/src/__test__/appEvents.test.ts
===================================================================
--- package/src/__test__/appEvents.test.ts
+++ package/src/__test__/appEvents.test.ts
@@ -1,201 +0,0 @@
-import { appEvents, AppEvent } from '../appEvents';
-import { post } from '../queries';
-import { __requestAtlassianAsApp } from '@forge/api';
-
-// Mock the dependencies
-jest.mock('../queries', () => ({
- post: jest.fn()
-}));
-
-jest.mock('@forge/api', () => ({
- __requestAtlassianAsApp: jest.fn()
-}));
-
-describe('appEvents', () => {
- beforeEach(() => {
- // Clear all mocks before each test
- jest.clearAllMocks();
- });
-
- describe('publishEvent', () => {
- it('should call post with the correct parameters', async () => {
- // Arrange
- const event: AppEvent = { key: 'test-event' };
- const mockResponse = {
- ok: true,
- json: jest.fn().mockResolvedValue({
- failedEvents: []
- })
- };
-
- // Mock the post function to return a successful response
- (post as jest.Mock).mockResolvedValue(mockResponse);
-
- // Act
- const result = await appEvents.publish(event);
-
- // Assert
- expect(post).toHaveBeenCalledTimes(1);
- expect(post).toHaveBeenCalledWith(
- '/forge/events/v1/app-events',
- {
- events: [
- {
- key: event.key
- }
- ]
- },
- __requestAtlassianAsApp
- );
- expect(result).toEqual({ type: 'success', failedEvents: [] });
- });
-
- it('should return a success result for successful requests', async () => {
- // Arrange
- const event: AppEvent = { key: 'test-event' };
- const mockResponse = {
- ok: true,
- json: jest.fn().mockResolvedValue({
- failedEvents: []
- })
- };
-
- // Mock the post function to return a successful response
- (post as jest.Mock).mockResolvedValue(mockResponse);
-
- // Act
- const result = await appEvents.publish(event);
-
- // Assert
- expect(result).toEqual({ type: 'success', failedEvents: [] });
- });
-
- it('should return an error result for failed requests', async () => {
- // Arrange
- const event: AppEvent = { key: 'test-event' };
- const mockResponse = {
- ok: false,
- status: 500,
- json: jest.fn().mockResolvedValue({
- errorMessages: ['Internal server error']
- })
- };
-
- // Mock the post function to return a failed response
- (post as jest.Mock).mockResolvedValue(mockResponse);
-
- // Act
- const result = await appEvents.publish(event);
-
- // Assert
- expect(result).toEqual({
- type: 'error',
- errorType: 'SERVICE_ERROR',
- errorMessage: 'Internal server error'
- });
- });
-
- it('should handle validation errors', async () => {
- // Arrange
- const event: AppEvent = { key: 'test-event' };
- const mockResponse = {
- ok: false,
- status: 400,
- json: jest.fn().mockResolvedValue({
- errorMessages: ['Invalid event type']
- })
- };
-
- // Mock the post function to return a validation error response
- (post as jest.Mock).mockResolvedValue(mockResponse);
-
- // Act
- const result = await appEvents.publish(event);
-
- // Assert
- expect(result).toEqual({
- type: 'error',
- errorType: 'VALIDATION_ERROR',
- errorMessage: 'Invalid event type'
- });
- });
-
- it('should handle errors object in the error response', async () => {
- // Arrange
- const event: AppEvent = { key: 'test-event' };
- const errorResponse = {
- errorMessages: [],
- errors: { key: 'Event key is required' }
- };
-
- const mockResponse = {
- ok: false,
- status: 400,
- json: jest.fn().mockResolvedValue(errorResponse)
- };
-
- // Mock the post function to return an error response with errors object
- (post as jest.Mock).mockResolvedValue(mockResponse);
-
- // Act
- const result = await appEvents.publish(event);
-
- // Assert
- expect(result).toEqual({
- type: 'error',
- errorType: 'VALIDATION_ERROR',
- errorMessage: 'key: Event key is required'
- });
- });
-
- it('should use OTHER as errorType for unknown status codes', async () => {
- // Arrange
- const event: AppEvent = { key: 'test-event' };
- const mockResponse = {
- ok: false,
- status: 418, // I'm a teapot
- json: jest.fn().mockResolvedValue({
- errorMessages: ['I refuse to brew coffee']
- })
- };
-
- // Mock the post function to return a response with unknown status code
- (post as jest.Mock).mockResolvedValue(mockResponse);
-
- // Act
- const result = await appEvents.publish(event);
-
- // Assert
- expect(result).toEqual({
- type: 'error',
- errorType: 'OTHER',
- errorMessage: 'I refuse to brew coffee'
- });
- });
-
- it('should handle empty error messages', async () => {
- // Arrange
- const event: AppEvent = { key: 'test-event' };
- const mockResponse = {
- ok: false,
- status: 500,
- json: jest.fn().mockResolvedValue({
- errorMessages: []
- })
- };
-
- // Mock the post function to return a response with empty error messages
- (post as jest.Mock).mockResolvedValue(mockResponse);
-
- // Act
- const result = await appEvents.publish(event);
-
- // Assert
- expect(result).toEqual({
- type: 'error',
- errorType: 'SERVICE_ERROR',
- errorMessage: '{"errorMessages":[]}'
- });
- });
- });
-});