npm package diff

Package: @forge/react

Versions: 10.8.0 - 10.9.0-next.0

File: package/out/__test__/components/utils/replaceUnsupportedDocumentNodes.test.js

Index: package/out/__test__/components/utils/replaceUnsupportedDocumentNodes.test.js
===================================================================
--- package/out/__test__/components/utils/replaceUnsupportedDocumentNodes.test.js
+++ package/out/__test__/components/utils/replaceUnsupportedDocumentNodes.test.js
@@ -0,0 +1,367 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+const replaceUnsupportedDocumentNodes_1 = require("../../../components/utils/replaceUnsupportedDocumentNodes");
+const emojiDocumentExample = {
+    type: 'doc',
+    version: 1,
+    content: [
+        {
+            type: 'paragraph',
+            content: [
+                {
+                    type: 'emoji',
+                    attrs: {
+                        shortName: ':awthanks:',
+                        id: 'atlassian-awthanks',
+                        text: ':awthanks:'
+                    }
+                },
+                {
+                    type: 'emoji',
+                    attrs: {
+                        shortName: ':customer-hello:',
+                        id: '2b851998-539e-443d-9b50-e4a210a3bf79',
+                        text: ':hello:'
+                    }
+                }
+            ]
+        }
+    ]
+};
+const mediaDocumentExample = {
+    type: 'doc',
+    version: 1,
+    content: [
+        {
+            type: 'mediaSingle',
+            content: [
+                {
+                    type: 'media',
+                    attrs: {
+                        id: '4478e39c-cf9b-41d1-ba92-68589487cd75',
+                        type: 'file',
+                        collection: 'MediaServicesSample',
+                        alt: 'moon.jpeg',
+                        width: 225,
+                        height: 225
+                    }
+                }
+            ]
+        },
+        {
+            type: 'mediaGroup',
+            content: [
+                {
+                    type: 'media',
+                    attrs: {
+                        id: '4478e39c-cf9b-41d1-ba92-68589487cd75',
+                        type: 'file',
+                        collection: 'MediaServicesSample',
+                        alt: 'moon.jpeg',
+                        width: 225,
+                        height: 225
+                    }
+                },
+                {
+                    type: 'media',
+                    attrs: {
+                        id: '4478e39c-cf9b-41d1-ba92-68589487cd75',
+                        type: 'file',
+                        collection: 'MediaServicesSample',
+                        alt: 'moon.jpeg',
+                        width: 225,
+                        height: 225
+                    }
+                }
+            ]
+        }
+    ]
+};
+const extensionDocumentExample = {
+    type: 'doc',
+    version: 1,
+    content: [
+        {
+            type: 'extension',
+            attrs: {
+                extensionType: 'com.atlassian.fabric',
+                extensionKey: 'clock'
+            },
+            content: [
+                {
+                    type: 'paragraph',
+                    content: [
+                        {
+                            type: 'text',
+                            text: 'This is the default content of the extension'
+                        }
+                    ]
+                }
+            ]
+        },
+        {
+            type: 'inlineExtension',
+            attrs: {
+                extensionType: 'com.atlassian.fabric',
+                extensionKey: 'clock'
+            },
+            content: [
+                {
+                    type: 'paragraph',
+                    content: [
+                        {
+                            type: 'text',
+                            text: 'This is the default content of the extension'
+                        }
+                    ]
+                }
+            ]
+        },
+        {
+            type: 'bodiedExtension',
+            attrs: {
+                extensionType: 'com.atlassian.fabric',
+                extensionKey: 'clock'
+            },
+            content: [
+                {
+                    type: 'paragraph',
+                    content: [
+                        {
+                            type: 'text',
+                            text: 'This is the default content of the extension'
+                        }
+                    ]
+                }
+            ]
+        }
+    ]
+};
+describe('replaceUnsupportedDocumentNodes', () => {
+    describe('replacing unsupported nodes with supported ones', () => {
+        const replaceUnsupportedNode = (node) => {
+            return {
+                type: 'paragraph',
+                content: [
+                    {
+                        type: 'text',
+                        text: `Unsupported content: ${node.type}`
+                    }
+                ]
+            };
+        };
+        it('should replace unsupported emoji with supported nodes', () => {
+            const replacedDocument = (0, replaceUnsupportedDocumentNodes_1.replaceUnsupportedDocumentNodes)(emojiDocumentExample, replaceUnsupportedNode);
+            const expectedDocument = {
+                content: [
+                    {
+                        content: [
+                            {
+                                content: [
+                                    {
+                                        text: 'Unsupported content: emoji',
+                                        type: 'text'
+                                    }
+                                ],
+                                type: 'paragraph'
+                            },
+                            {
+                                content: [
+                                    {
+                                        text: 'Unsupported content: emoji',
+                                        type: 'text'
+                                    }
+                                ],
+                                type: 'paragraph'
+                            }
+                        ],
+                        type: 'paragraph'
+                    }
+                ],
+                type: 'doc',
+                version: 1
+            };
+            expect(replacedDocument).toEqual(expectedDocument);
+        });
+        it('should replace unsupported media with supported nodes', () => {
+            const replacedDocument = (0, replaceUnsupportedDocumentNodes_1.replaceUnsupportedDocumentNodes)(mediaDocumentExample, replaceUnsupportedNode);
+            const expectedDocument = {
+                type: 'doc',
+                version: 1,
+                content: [
+                    {
+                        content: [
+                            {
+                                content: [
+                                    {
+                                        text: 'Unsupported content: media',
+                                        type: 'text'
+                                    }
+                                ],
+                                type: 'paragraph'
+                            }
+                        ],
+                        type: 'mediaSingle'
+                    },
+                    {
+                        content: [
+                            {
+                                content: [
+                                    {
+                                        text: 'Unsupported content: media',
+                                        type: 'text'
+                                    }
+                                ],
+                                type: 'paragraph'
+                            },
+                            {
+                                content: [
+                                    {
+                                        text: 'Unsupported content: media',
+                                        type: 'text'
+                                    }
+                                ],
+                                type: 'paragraph'
+                            }
+                        ],
+                        type: 'mediaGroup'
+                    }
+                ]
+            };
+            expect(replacedDocument).toEqual(expectedDocument);
+        });
+        it('should replace unsupported extension with supported nodes', () => {
+            const replacedDocument = (0, replaceUnsupportedDocumentNodes_1.replaceUnsupportedDocumentNodes)(extensionDocumentExample, replaceUnsupportedNode);
+            const expectedDocument = {
+                type: 'doc',
+                version: 1,
+                content: [
+                    {
+                        content: [
+                            {
+                                text: 'Unsupported content: extension',
+                                type: 'text'
+                            }
+                        ],
+                        type: 'paragraph'
+                    },
+                    {
+                        content: [
+                            {
+                                text: 'Unsupported content: inlineExtension',
+                                type: 'text'
+                            }
+                        ],
+                        type: 'paragraph'
+                    },
+                    {
+                        content: [
+                            {
+                                text: 'Unsupported content: bodiedExtension',
+                                type: 'text'
+                            }
+                        ],
+                        type: 'paragraph'
+                    }
+                ]
+            };
+            expect(replacedDocument).toEqual(expectedDocument);
+        });
+    });
+    it('should not change document when it only has supported nodes', () => {
+        const replaceUnsupportedNode = (node) => {
+            return {
+                type: 'paragraph',
+                content: [
+                    {
+                        type: 'text',
+                        text: `Unsupported content: ${node.type}`
+                    }
+                ]
+            };
+        };
+        const documentToRender = {
+            type: 'doc',
+            version: 1,
+            content: [
+                {
+                    type: 'paragraph',
+                    content: [
+                        {
+                            type: 'text',
+                            text: 'Supported node'
+                        },
+                        {
+                            type: 'emoji',
+                            attrs: {
+                                shortName: ':grinning:',
+                                text: '😀'
+                            }
+                        }
+                    ]
+                }
+            ]
+        };
+        const replacedDocument = (0, replaceUnsupportedDocumentNodes_1.replaceUnsupportedDocumentNodes)(documentToRender, replaceUnsupportedNode);
+        expect(replacedDocument).toEqual(documentToRender);
+    });
+    describe('drop unsupported nodes when Visitor returns false', () => {
+        const replaceUnsupportedNode = () => false;
+        it('should drop unsupported emoji nodes', () => {
+            const replacedDocument = (0, replaceUnsupportedDocumentNodes_1.replaceUnsupportedDocumentNodes)(emojiDocumentExample, replaceUnsupportedNode);
+            const expectedDocument = {
+                content: [
+                    {
+                        content: [],
+                        type: 'paragraph'
+                    }
+                ],
+                type: 'doc',
+                version: 1
+            };
+            expect(replacedDocument).toEqual(expectedDocument);
+        });
+        it('should drop unsupported media node', () => {
+            const replacedDocument = (0, replaceUnsupportedDocumentNodes_1.replaceUnsupportedDocumentNodes)(mediaDocumentExample, replaceUnsupportedNode);
+            const expectedDocument = {
+                type: 'doc',
+                version: 1,
+                content: [
+                    {
+                        content: [],
+                        type: 'mediaSingle'
+                    },
+                    {
+                        content: [],
+                        type: 'mediaGroup'
+                    }
+                ]
+            };
+            expect(replacedDocument).toEqual(expectedDocument);
+        });
+        it('should drop unsupported extension nodes', () => {
+            const replacedDocument = (0, replaceUnsupportedDocumentNodes_1.replaceUnsupportedDocumentNodes)(extensionDocumentExample, replaceUnsupportedNode);
+            const expectedDocument = {
+                type: 'doc',
+                version: 1,
+                content: []
+            };
+            expect(replacedDocument).toEqual(expectedDocument);
+        });
+    });
+    describe('Keep original nodes when Visitor returns undefined', () => {
+        const replaceUnsupportedNode = () => undefined;
+        it('should use original emoji nodes', () => {
+            const replacedDocument = (0, replaceUnsupportedDocumentNodes_1.replaceUnsupportedDocumentNodes)(emojiDocumentExample, replaceUnsupportedNode);
+            expect(replacedDocument).toEqual(emojiDocumentExample);
+        });
+        it('should use original media node', () => {
+            const replacedDocument = (0, replaceUnsupportedDocumentNodes_1.replaceUnsupportedDocumentNodes)(mediaDocumentExample, replaceUnsupportedNode);
+            expect(replacedDocument).toEqual(mediaDocumentExample);
+        });
+        it('should use original extension nodes', () => {
+            const replacedDocument = (0, replaceUnsupportedDocumentNodes_1.replaceUnsupportedDocumentNodes)(extensionDocumentExample, replaceUnsupportedNode);
+            expect(replacedDocument).toEqual(extensionDocumentExample);
+        });
+    });
+});