@kezios/forest-express-decorator
Version:
🚀 Decorators for Express Forest Admin
125 lines (124 loc) • 6.14 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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 };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable jest/expect-expect */
const express_1 = __importDefault(require("express"));
const supertest_1 = __importDefault(require("supertest"));
const Liana = __importStar(require("../test-utils/PermissionMiddlewareCreator"));
const SmartAction_1 = require("../types/SmartAction");
const Post_1 = require("./sample/empty-collection/Post");
const Post_2 = require("./sample/smart-field-collection/Post");
const Post_3 = require("./sample/collection-with-smart-action/Post");
const InvalidPost_1 = require("./sample/collection-with-smart-action/InvalidPost");
const AddLike_1 = require("./sample/empty-smart-action/AddLike");
const AddLike_2 = require("./sample/collection-with-smart-action/AddLike");
const AddLike_3 = require("./sample/smart-action-with-fields/AddLike");
describe('Decorator', () => {
describe('Collection', () => {
it('initialize collection', () => {
const post = new Post_1.Post();
expect(post.name).toBe('post');
});
it('initialize collection with smartActions', () => {
const post = new Post_3.Post();
expect(Object.keys(post.actions)).toHaveLength(1);
expect(post.actions['add-like']).toBeInstanceOf(AddLike_2.AddLike);
});
it('should create route for smart-action endpoint', () => __awaiter(void 0, void 0, void 0, function* () {
const app = (0, express_1.default)();
const post = new Post_3.Post();
const config = post.initialize(app, Liana);
expect(config.actions).toEqual([
expect.objectContaining({
name: 'Ajouter un like',
type: 'single',
fields: [
expect.objectContaining({
name: 'nb',
field: 'Nombre de likes à ajouter',
type: 'Number',
hook: 'onNbChange'
}),
{
name: 'date',
field: 'Date',
type: 'DateOnly'
}
]
})
]);
expect(config.actions[0].hooks).toHaveProperty('change');
expect(config.actions[0].hooks.change).toHaveProperty('onNbChange');
yield (0, supertest_1.default)(app)
.post(`/post/smart-action/add-like`)
.expect(201);
}));
it('should throw error if called initialize without name defined', () => {
const app = (0, express_1.default)();
const invalidPost = new InvalidPost_1.InvalidPost();
expect(() => {
invalidPost.initialize(app, Liana);
}).toThrowError("Cannot initialize collection with undefined name");
});
});
describe('SmartField', () => {
it('initialize collection with', () => {
const post = new Post_2.Post();
expect(post.fields).toHaveLength(1);
expect(post.fields[0]).toHaveProperty('field', 'popularity');
expect(post.fields[0]).toHaveProperty('type', 'String');
expect(post.fields[0].get()).toBe('2/10');
});
});
describe('SmartAction', () => {
it('initialize smart action', () => {
const addLike = new AddLike_1.AddLike();
expect(addLike.label).toBe('Ajouter un like');
expect(addLike.type).toBe(SmartAction_1.SMART_ACTION_TYPE.SINGLE);
});
it('initialize smart action with field', () => {
const addLike = new AddLike_3.AddLike();
expect(addLike.label).toBe('Ajouter un like');
expect(addLike.type).toBe(SmartAction_1.SMART_ACTION_TYPE.SINGLE);
expect(addLike.fields).toHaveLength(2);
expect(addLike.fields[0]).toHaveProperty('field', 'Nombre de likes à ajouter');
expect(addLike.fields[0]).toHaveProperty('type', 'Number');
expect(addLike.fields[1]).toHaveProperty('field', 'Date');
expect(addLike.fields[1]).toHaveProperty('type', 'DateOnly');
});
});
});