Skip to content

Commit

Permalink
test: adding unit tests for queue wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dzehnder committed Oct 20, 2023
1 parent 42dee05 commit a873045
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions test/queue-wrapper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you 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 REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-env mocha */

import assert from 'assert';
import queueWrapper from '../src/queue-wrapper.js';
import SQSQueue from '../src/sqs-queue.js';

describe('Queue Wrapper Tests', () => {
let mockFunc;
let mockRequest;
let mockContext;

beforeEach(() => {
mockFunc = async () => {};
mockRequest = {};
mockContext = {
env: {
AUDIT_RESULTS_QUEUE_URL: 'queue-url',
},
attributes: {},
region: 'test-region',
log: {
info: () => {},
error: () => {},
},
};
});

it('should throw error if queue url is not provided', async () => {
mockContext.env.AUDIT_RESULTS_QUEUE_URL = null;

try {
await queueWrapper(mockFunc)(mockRequest, mockContext);
assert.fail('Expected error to be thrown');
} catch (error) {
assert.strictEqual(error.message, 'AUDIT_RESULTS_QUEUE_URL env variable is empty/not provided');
}
});

it('should set queue url in context attributes', async () => {
await queueWrapper(mockFunc)(mockRequest, mockContext);
assert.strictEqual(mockContext.attributes.queueUrl, 'queue-url');
});

it('should create queue if not present in context', async () => {
await queueWrapper(mockFunc)(mockRequest, mockContext);
assert(mockContext.queue instanceof SQSQueue, 'context.queue was not correctly instantiated.');
});

it('should not re-initialize queue if already present in context', async () => {
const mockQueue = new SQSQueue(mockContext);
mockContext.queue = mockQueue;

await queueWrapper(mockFunc)(mockRequest, mockContext);

assert.strictEqual(mockContext.queue, mockQueue, 'context.queue was re-initialized.');
});
});

0 comments on commit a873045

Please sign in to comment.