npm package diff
Package: @forge/events
Versions: 1.0.3-next.0 - 1.0.3-next.0-experimental-f44d099
File: package/out/__test__/appEvents.test.js
Index: package/out/__test__/appEvents.test.js
===================================================================
--- package/out/__test__/appEvents.test.js
+++ package/out/__test__/appEvents.test.js
@@ -0,0 +1,127 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+const appEvents_1 = require("../appEvents");
+const queries_1 = require("../queries");
+const api_1 = require("@forge/api");
+jest.mock('../queries', () => ({
+ post: jest.fn()
+}));
+jest.mock('@forge/api', () => ({
+ __requestAtlassianAsApp: jest.fn()
+}));
+describe('appEvents', () => {
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+ describe('publishEvent', () => {
+ it('should call post with the correct parameters', async () => {
+ const event = { key: 'test-event' };
+ const mockResponse = { ok: true, json: jest.fn() };
+ queries_1.post.mockResolvedValue(mockResponse);
+ const result = await appEvents_1.appEvents.publish(event);
+ expect(queries_1.post).toHaveBeenCalledTimes(1);
+ expect(queries_1.post).toHaveBeenCalledWith('/forge/events/v1/app-events', {
+ events: [
+ {
+ eventKey: event.key
+ }
+ ]
+ }, api_1.__requestAtlassianAsApp);
+ expect(result).toEqual({ type: 'success' });
+ });
+ it('should return a success result for successful requests', async () => {
+ const event = { key: 'test-event' };
+ const mockResponse = { ok: true, json: jest.fn() };
+ queries_1.post.mockResolvedValue(mockResponse);
+ const result = await appEvents_1.appEvents.publish(event);
+ expect(result).toEqual({ type: 'success' });
+ });
+ it('should return an error result for failed requests', async () => {
+ const event = { key: 'test-event' };
+ const mockResponse = {
+ ok: false,
+ status: 500,
+ json: jest.fn().mockResolvedValue({
+ errorMessages: ['Internal server error']
+ })
+ };
+ queries_1.post.mockResolvedValue(mockResponse);
+ const result = await appEvents_1.appEvents.publish(event);
+ expect(result).toEqual({
+ type: 'error',
+ errorType: 'SERVICE_ERROR',
+ errorMessage: 'Internal server error'
+ });
+ });
+ it('should handle validation errors', async () => {
+ const event = { key: 'test-event' };
+ const mockResponse = {
+ ok: false,
+ status: 400,
+ json: jest.fn().mockResolvedValue({
+ errorMessages: ['Invalid event type']
+ })
+ };
+ queries_1.post.mockResolvedValue(mockResponse);
+ const result = await appEvents_1.appEvents.publish(event);
+ expect(result).toEqual({
+ type: 'error',
+ errorType: 'VALIDATION_ERROR',
+ errorMessage: 'Invalid event type'
+ });
+ });
+ it('should handle errors object in the error response', async () => {
+ const event = { key: 'test-event' };
+ const errorResponse = {
+ errorMessages: [],
+ errors: { eventKey: 'Event key is required' }
+ };
+ const mockResponse = {
+ ok: false,
+ status: 400,
+ json: jest.fn().mockResolvedValue(errorResponse)
+ };
+ queries_1.post.mockResolvedValue(mockResponse);
+ const result = await appEvents_1.appEvents.publish(event);
+ expect(result).toEqual({
+ type: 'error',
+ errorType: 'VALIDATION_ERROR',
+ errorMessage: 'eventKey: Event key is required'
+ });
+ });
+ it('should use OTHER as errorType for unknown status codes', async () => {
+ const event = { key: 'test-event' };
+ const mockResponse = {
+ ok: false,
+ status: 418,
+ json: jest.fn().mockResolvedValue({
+ errorMessages: ['I refuse to brew coffee']
+ })
+ };
+ queries_1.post.mockResolvedValue(mockResponse);
+ const result = await appEvents_1.appEvents.publish(event);
+ expect(result).toEqual({
+ type: 'error',
+ errorType: 'OTHER',
+ errorMessage: 'I refuse to brew coffee'
+ });
+ });
+ it('should handle empty error messages', async () => {
+ const event = { key: 'test-event' };
+ const mockResponse = {
+ ok: false,
+ status: 500,
+ json: jest.fn().mockResolvedValue({
+ errorMessages: []
+ })
+ };
+ queries_1.post.mockResolvedValue(mockResponse);
+ const result = await appEvents_1.appEvents.publish(event);
+ expect(result).toEqual({
+ type: 'error',
+ errorType: 'SERVICE_ERROR',
+ errorMessage: '{"errorMessages":[]}'
+ });
+ });
+ });
+});