"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sanitize_1 = __importDefault(require("./sanitize"));
const errors_1 = require("../../errors");
describe('sanitizeFields', () => {
    it('should throw on missing type field', () => {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        const fields = [{
                label: 'some-collection',
                name: 'Some Collection',
            }];
        expect(() => {
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            (0, sanitize_1.default)(fields, []);
        }).toThrow(errors_1.MissingFieldType);
    });
    it("should throw on invalid field name", () => {
        const fields = [
            {
                label: "some.collection",
                name: "some.collection",
                type: "text",
            }
        ];
        expect(() => {
            (0, sanitize_1.default)(fields, []);
        }).toThrow(errors_1.InvalidFieldName);
    });
    describe('auto-labeling', () => {
        it('should populate label if missing', () => {
            const fields = [{
                    name: 'someField',
                    type: 'text',
                }];
            const sanitizedField = (0, sanitize_1.default)(fields, [])[0];
            expect(sanitizedField.name).toStrictEqual('someField');
            expect(sanitizedField.label).toStrictEqual('Some Field');
            expect(sanitizedField.type).toStrictEqual('text');
        });
        it('should allow auto-label override', () => {
            const fields = [{
                    name: 'someField',
                    type: 'text',
                    label: 'Do not label',
                }];
            const sanitizedField = (0, sanitize_1.default)(fields, [])[0];
            expect(sanitizedField.name).toStrictEqual('someField');
            expect(sanitizedField.label).toStrictEqual('Do not label');
            expect(sanitizedField.type).toStrictEqual('text');
        });
        describe('opt-out', () => {
            it('should allow label opt-out', () => {
                const fields = [{
                        name: 'someField',
                        type: 'text',
                        label: false,
                    }];
                const sanitizedField = (0, sanitize_1.default)(fields, [])[0];
                expect(sanitizedField.name).toStrictEqual('someField');
                expect(sanitizedField.label).toStrictEqual(false);
                expect(sanitizedField.type).toStrictEqual('text');
            });
            it('should allow label opt-out for arrays', () => {
                const arrayField = {
                    name: 'items',
                    type: 'array',
                    label: false,
                    fields: [
                        {
                            name: 'itemName',
                            type: 'text',
                        },
                    ],
                };
                const sanitizedField = (0, sanitize_1.default)([arrayField], [])[0];
                expect(sanitizedField.name).toStrictEqual('items');
                expect(sanitizedField.label).toStrictEqual(false);
                expect(sanitizedField.type).toStrictEqual('array');
                expect(sanitizedField.labels).toBeUndefined();
            });
            it('should allow label opt-out for blocks', () => {
                const fields = [{
                        name: 'noLabelBlock',
                        type: 'blocks',
                        label: false,
                        blocks: [
                            {
                                slug: 'number',
                                fields: [
                                    {
                                        name: 'testNumber',
                                        type: 'number',
                                    },
                                ],
                            },
                        ],
                    }];
                const sanitizedField = (0, sanitize_1.default)(fields, [])[0];
                expect(sanitizedField.name).toStrictEqual('noLabelBlock');
                expect(sanitizedField.label).toStrictEqual(false);
                expect(sanitizedField.type).toStrictEqual('blocks');
                expect(sanitizedField.labels).toBeUndefined();
            });
        });
        it('should label arrays with plural and singular', () => {
            const fields = [{
                    name: 'items',
                    type: 'array',
                    fields: [
                        {
                            name: 'itemName',
                            type: 'text',
                        },
                    ],
                }];
            const sanitizedField = (0, sanitize_1.default)(fields, [])[0];
            expect(sanitizedField.name).toStrictEqual('items');
            expect(sanitizedField.label).toStrictEqual('Items');
            expect(sanitizedField.type).toStrictEqual('array');
            expect(sanitizedField.labels).toMatchObject({ singular: 'Item', plural: 'Items' });
        });
        it('should label blocks with plural and singular', () => {
            const fields = [{
                    name: 'specialBlock',
                    type: 'blocks',
                    blocks: [
                        {
                            slug: 'number',
                            fields: [{ name: 'testNumber', type: 'number' }],
                        },
                    ],
                }];
            const sanitizedField = (0, sanitize_1.default)(fields, [])[0];
            expect(sanitizedField.name).toStrictEqual('specialBlock');
            expect(sanitizedField.label).toStrictEqual('Special Block');
            expect(sanitizedField.type).toStrictEqual('blocks');
            expect(sanitizedField.labels).toMatchObject({ singular: 'Special Block', plural: 'Special Blocks' });
            expect(sanitizedField.blocks[0].fields[0].label).toStrictEqual('Test Number');
        });
    });
    describe('relationships', () => {
        it('should not throw on valid relationship', () => {
            const validRelationships = ['some-collection'];
            const fields = [{
                    type: 'relationship',
                    label: 'my-relationship',
                    name: 'My Relationship',
                    relationTo: 'some-collection',
                }];
            expect(() => {
                (0, sanitize_1.default)(fields, validRelationships);
            }).not.toThrow();
        });
        it('should not throw on valid relationship - multiple', () => {
            const validRelationships = ['some-collection', 'another-collection'];
            const fields = [{
                    type: 'relationship',
                    label: 'my-relationship',
                    name: 'My Relationship',
                    relationTo: ['some-collection', 'another-collection'],
                }];
            expect(() => {
                (0, sanitize_1.default)(fields, validRelationships);
            }).not.toThrow();
        });
        it('should not throw on valid relationship inside blocks', () => {
            const validRelationships = ['some-collection'];
            const relationshipBlock = {
                slug: 'relationshipBlock',
                fields: [{
                        type: 'relationship',
                        label: 'my-relationship',
                        name: 'My Relationship',
                        relationTo: 'some-collection',
                    }],
            };
            const fields = [{
                    name: 'layout',
                    label: 'Layout Blocks',
                    type: 'blocks',
                    blocks: [relationshipBlock],
                }];
            expect(() => {
                (0, sanitize_1.default)(fields, validRelationships);
            }).not.toThrow();
        });
        it('should throw on invalid relationship', () => {
            const validRelationships = ['some-collection'];
            const fields = [{
                    type: 'relationship',
                    label: 'my-relationship',
                    name: 'My Relationship',
                    relationTo: 'not-valid',
                }];
            expect(() => {
                (0, sanitize_1.default)(fields, validRelationships);
            }).toThrow(errors_1.InvalidFieldRelationship);
        });
        it('should throw on invalid relationship - multiple', () => {
            const validRelationships = ['some-collection', 'another-collection'];
            const fields = [{
                    type: 'relationship',
                    label: 'my-relationship',
                    name: 'My Relationship',
                    relationTo: ['some-collection', 'not-valid'],
                }];
            expect(() => {
                (0, sanitize_1.default)(fields, validRelationships);
            }).toThrow(errors_1.InvalidFieldRelationship);
        });
        it('should throw on invalid relationship inside blocks', () => {
            const validRelationships = ['some-collection'];
            const relationshipBlock = {
                slug: 'relationshipBlock',
                fields: [{
                        type: 'relationship',
                        label: 'my-relationship',
                        name: 'My Relationship',
                        relationTo: 'not-valid',
                    }],
            };
            const fields = [{
                    name: 'layout',
                    label: 'Layout Blocks',
                    type: 'blocks',
                    blocks: [relationshipBlock],
                }];
            expect(() => {
                (0, sanitize_1.default)(fields, validRelationships);
            }).toThrow(errors_1.InvalidFieldRelationship);
        });
        it('should defaultValue of checkbox to false if required and undefined', () => {
            const fields = [{
                    type: 'checkbox',
                    name: 'My Checkbox',
                    required: true,
                }];
            const sanitizedField = (0, sanitize_1.default)(fields, [])[0];
            expect(sanitizedField.defaultValue).toStrictEqual(false);
        });
        it('should return empty field array if no fields', () => {
            const sanitizedFields = (0, sanitize_1.default)([], []);
            expect(sanitizedFields).toStrictEqual([]);
        });
    });
});
//# sourceMappingURL=data:application/json;base64, 