@shopware-ag/meteor-admin-sdk
Version:
The Meteor SDK for the Shopware Administration.
194 lines • 8.9 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const mockStatusSender = jest.fn();
const mockRequestSender = jest.fn();
let mockConsentRequestId;
const mockCreateSender = jest.fn((messageType, options) => {
if (messageType === 'consentStatus') {
return mockStatusSender;
}
if (messageType === 'consentRequest') {
mockConsentRequestId = options === null || options === void 0 ? void 0 : options.requestId;
return mockRequestSender;
}
throw new Error(`Unexpected sender type: ${messageType}`);
});
const mockUnhandle = jest.fn();
let mockConsentRequestResponseHandler;
const mockCreateHandler = jest.fn((messageType) => {
if (messageType !== 'consentRequestResponse') {
throw new Error(`Unexpected handler type: ${messageType}`);
}
return (callback) => {
mockConsentRequestResponseHandler = callback;
return mockUnhandle;
};
});
jest.mock('../channel', () => ({
createSender: (messageType, options) => mockCreateSender(messageType, options),
createHandler: (messageType) => mockCreateHandler(messageType),
}));
import { request, status } from './index';
describe('consent', () => {
beforeEach(() => {
mockStatusSender.mockReset();
mockRequestSender.mockReset();
mockCreateSender.mockClear();
mockCreateHandler.mockClear();
mockUnhandle.mockReset();
mockConsentRequestResponseHandler = undefined;
mockConsentRequestId = undefined;
mockUnhandle.mockImplementation(() => {
mockConsentRequestResponseHandler = undefined;
});
});
describe('status', () => {
it('sends the consent status request and maps the response', () => __awaiter(void 0, void 0, void 0, function* () {
mockStatusSender.mockResolvedValue({
name: 'newsletter',
status: 'accepted',
updatedAt: '2026-04-13T10:00:00.000Z',
acceptedRevision: '2',
latestRevision: '3',
});
const consent = yield status({
consent: 'newsletter',
});
expect(mockCreateSender).toHaveBeenCalledWith('consentStatus', undefined);
expect(mockStatusSender).toHaveBeenCalledWith({
consent: 'newsletter',
});
expect(consent).toMatchObject({
name: 'newsletter',
status: 'accepted',
updatedAt: '2026-04-13T10:00:00.000Z',
acceptedRevision: '2',
lastRevision: '3',
});
expect(consent.isAccepted).toBe(true);
expect(consent.isStale).toBe(true);
}));
});
describe('request', () => {
it('registers the response handler before sending the consent request', () => __awaiter(void 0, void 0, void 0, function* () {
mockRequestSender.mockResolvedValue(undefined);
const { requestPromise } = request({
consent: 'newsletter',
requestMessage: 'Please confirm',
privacyLink: 'https://example.com/privacy',
});
expect(mockCreateHandler).toHaveBeenCalledWith('consentRequestResponse');
expect(mockCreateSender).toHaveBeenCalledWith('consentRequest', expect.objectContaining({
requestId: expect.any(String),
}));
expect(mockRequestSender).toHaveBeenCalledWith({
consent: 'newsletter',
requestMessage: 'Please confirm',
privacyLink: 'https://example.com/privacy',
});
expect(mockConsentRequestId).toEqual(expect.any(String));
expect(mockConsentRequestResponseHandler).toEqual(expect.any(Function));
yield (mockConsentRequestResponseHandler === null || mockConsentRequestResponseHandler === void 0 ? void 0 : mockConsentRequestResponseHandler({
name: 'newsletter',
requestId: mockConsentRequestId,
consent: {
name: 'newsletter',
status: 'accepted',
updatedAt: '2026-04-13T11:00:00.000Z',
acceptedRevision: '3',
latestRevision: '3',
},
}));
const consent = yield requestPromise;
expect(consent).toMatchObject({
name: 'newsletter',
status: 'accepted',
acceptedRevision: '3',
lastRevision: '3',
});
expect(consent.isAccepted).toBe(true);
expect(consent.isStale).toBe(false);
expect(mockUnhandle).toHaveBeenCalledTimes(1);
}));
it('ignores non-matching consent responses', () => __awaiter(void 0, void 0, void 0, function* () {
mockRequestSender.mockResolvedValue(undefined);
const { requestPromise } = request({
consent: 'newsletter',
});
yield (mockConsentRequestResponseHandler === null || mockConsentRequestResponseHandler === void 0 ? void 0 : mockConsentRequestResponseHandler({
name: 'terms-and-conditions',
requestId: mockConsentRequestId,
consent: {
name: 'terms-and-conditions',
status: 'declined',
updatedAt: null,
acceptedRevision: null,
latestRevision: '1',
},
}));
let isResolved = false;
void requestPromise.then(() => {
isResolved = true;
});
yield Promise.resolve();
expect(isResolved).toBe(false);
expect(mockUnhandle).not.toHaveBeenCalled();
yield (mockConsentRequestResponseHandler === null || mockConsentRequestResponseHandler === void 0 ? void 0 : mockConsentRequestResponseHandler({
name: 'newsletter',
requestId: mockConsentRequestId,
consent: {
name: 'newsletter',
status: 'accepted',
updatedAt: null,
acceptedRevision: '1',
latestRevision: '1',
},
}));
yield expect(requestPromise).resolves.toMatchObject({
name: 'newsletter',
status: 'accepted',
});
expect(mockUnhandle).toHaveBeenCalledTimes(1);
}));
it('rejects and unregisters the response handler when sending fails', () => __awaiter(void 0, void 0, void 0, function* () {
const sendError = new Error('request failed');
mockRequestSender.mockRejectedValue(sendError);
const { requestPromise } = request({
consent: 'newsletter',
});
yield expect(requestPromise).rejects.toThrow('request failed');
expect(mockUnhandle).toHaveBeenCalledTimes(1);
}));
it('aborts and unregisters the response handler so no further messages are handled', () => __awaiter(void 0, void 0, void 0, function* () {
mockRequestSender.mockResolvedValue(undefined);
const { requestPromise, abort } = request({
consent: 'newsletter',
});
const abortError = new Error('request aborted');
abort(abortError);
expect(mockUnhandle).toHaveBeenCalledTimes(1);
expect(mockConsentRequestResponseHandler).toBeUndefined();
yield expect(requestPromise).rejects.toThrow('request aborted');
const handlerAfterAbort = mockConsentRequestResponseHandler;
yield (handlerAfterAbort === null || handlerAfterAbort === void 0 ? void 0 : handlerAfterAbort({
name: 'newsletter',
consent: {
name: 'newsletter',
status: 'accepted',
updatedAt: null,
acceptedRevision: '1',
latestRevision: '1',
},
}));
expect(mockUnhandle).toHaveBeenCalledTimes(1);
}));
});
});
//# sourceMappingURL=index.spec.js.map