UNPKG

@shopware-ag/meteor-admin-sdk

Version:

The Meteor SDK for the Shopware Administration.

134 lines 6.92 kB
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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "vue", "flush-promises", "./useDataset", "../index"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable @typescript-eslint/no-empty-function */ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/await-thenable */ /* eslint-disable @typescript-eslint/no-unsafe-return */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/explicit-function-return-type */ /* eslint-disable @typescript-eslint/no-explicit-any */ const vue_1 = __importDefault(require("vue")); const flush_promises_1 = __importDefault(require("flush-promises")); const useDataset_1 = require("./useDataset"); const index_1 = require("../index"); vue_1.default.config.devtools = false; vue_1.default.config.productionTip = false; // Mock the data methods jest.mock('../index', () => ({ get: jest.fn(), subscribe: jest.fn(), update: jest.fn(), })); const getMock = index_1.get; const subscribeMock = index_1.subscribe; const updateMock = index_1.update; function mockLoadComposableInApp(composable) { let result; const app = new vue_1.default({ setup() { result = composable(); return () => { }; }, }); app.$mount(document.createElement('div')); return [result, app]; } describe('useDataset composable', () => { beforeEach(() => { getMock.mockClear(); subscribeMock.mockClear(); updateMock.mockClear(); // Default mock implementations getMock.mockResolvedValue({ name: 'Initial Name' }); subscribeMock.mockReturnValue(() => { }); // Return an empty unsubscribe function updateMock.mockResolvedValue(undefined); }); it('should not be ready at the beginning and become ready afterwards', () => __awaiter(void 0, void 0, void 0, function* () { const [result, app] = mockLoadComposableInApp(() => (0, useDataset_1.useDataset)('product')); expect(result.isReady.value).toBe(false); yield (0, flush_promises_1.default)(); expect(result.isReady.value).toBe(true); app.$destroy(); })); it('should fetch initial data and update the state', () => __awaiter(void 0, void 0, void 0, function* () { const initialData = { name: 'Test Product' }; getMock.mockResolvedValue(initialData); const [result, app] = mockLoadComposableInApp(() => (0, useDataset_1.useDataset)('product')); expect(result.data.value).toBe(null); yield (0, flush_promises_1.default)(); expect(getMock).toHaveBeenCalledWith({ id: 'product', selectors: undefined }); expect(result.data.value).toEqual(initialData); app.$destroy(); })); it('should subscribe to data changes and update the state', () => __awaiter(void 0, void 0, void 0, function* () { let subscribeCallback = () => { }; subscribeMock.mockImplementation((_, callback) => { subscribeCallback = callback; return () => { }; }); const [result, app] = mockLoadComposableInApp(() => (0, useDataset_1.useDataset)('product')); yield (0, flush_promises_1.default)(); expect(result.data.value.name).toBe('Initial Name'); const updatedData = { name: 'Updated Name' }; subscribeCallback({ data: updatedData }); yield (0, flush_promises_1.default)(); expect(result.data.value).toEqual(updatedData); app.$destroy(); })); it('should call update when the reactive data is changed', () => __awaiter(void 0, void 0, void 0, function* () { const [result, app] = mockLoadComposableInApp(() => (0, useDataset_1.useDataset)('product')); yield (0, flush_promises_1.default)(); result.data.value.name = 'New Name'; yield (0, flush_promises_1.default)(); expect(updateMock).toHaveBeenCalledWith({ id: 'product', data: { name: 'New Name' } }); app.$destroy(); })); it('should not call update when data is updated via subscribe', () => __awaiter(void 0, void 0, void 0, function* () { let subscribeCallback = () => { }; subscribeMock.mockImplementation((_, callback) => { subscribeCallback = callback; return () => { }; }); const [result, app] = mockLoadComposableInApp(() => (0, useDataset_1.useDataset)('product')); yield (0, flush_promises_1.default)(); updateMock.mockClear(); const updatedData = { name: 'Updated via subscribe' }; subscribeCallback({ data: updatedData }); yield (0, flush_promises_1.default)(); expect(result.data.value).toEqual(updatedData); expect(updateMock).not.toHaveBeenCalled(); app.$destroy(); })); it('should call unsubscribe on unmount', () => __awaiter(void 0, void 0, void 0, function* () { const unsubscribeMock = jest.fn(); subscribeMock.mockReturnValue(unsubscribeMock); const [, app] = mockLoadComposableInApp(() => (0, useDataset_1.useDataset)('product')); yield (0, flush_promises_1.default)(); app.$destroy(); expect(unsubscribeMock).toHaveBeenCalled(); })); }); }); //# sourceMappingURL=useDataset.spec.js.map