You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My team and I are working on an Office Word Add-in which interacts with the document to insert text, images, tables, etc.
We've implemented a few helper functions to make those insertions with the Word JS API. Here's how one looks like:
exportconstinsertTagIntoDoc=async(tag: Tag)=>{const{ path, type }=tag;try{awaitWord.run(asynccontext=>{constselectionRange=context.document.getSelection();letinsertedContent: Word.Range|Word.InlinePicture|null=null;// Insert content into the documentif(type==='text'){insertedContent=selectionRange.insertText(`{{ ${path} }}`,Word.InsertLocation.replace);}else{insertedContent=selectionRange.insertInlinePictureFromBase64(tag.base64,Word.InsertLocation.replace);insertedContent.altTextDescription=tag.altText??'';if(tag.height){insertedContent.height=tag.height;}if(tag.width){insertedContent.width=tag.width;}}// Move cursor to the end of the inserted contentinsertedContent.select('End');awaitcontext.sync();});}catch(error){console.log(error)}};
However, it is not clear to us how to unit test those functions with the office-addin-mock. We know there is a section in the documentation about using that mock but it is very succinct and we don't know how to apply it to our use case.
More specifically, it's not clear what should be added the the mock object data and how to test that after calling the insertTagIntoDoc function, the document contains the data as expected, in this case, either some text or an image. Also, we want to test that the inserted context replace the current selection in the document, and that the cursor is move to the end of the inserted content.
import{OfficeMockObject}from'office-addin-mock';// Create the seed mock objectexportconstmockData={context: {document: {// what to add here?},},InsertLocation: {replace: 'replace',before: 'before',after: 'after',},asyncrun(callback: (context: unknown)=>Promise<void>){awaitcallback(this.context);},};// Create the final mock object from the seed objectexportconstwordMock=newOfficeMockObject(mockData);// Define and initialize the Word objectglobal.Word=wordMockasunknownastypeofWord;
Here's what we get when log the wordMock object in the console in our test:
it('should insert an image tag',async()=>{awaitinsertTagIntoDoc(imgTag);console.log(wordMock);// TODO: check document data});
OfficeMockObject{
_properties: Map(2){'context'=>OfficeMockObject{_properties: [Map],_loaded: false,_host: undefined,_value: 'Error, property was not loaded',_valueBeforeLoaded: undefined,_isObject: true,document: [OfficeMockObject]},'InsertLocation'=>OfficeMockObject{_properties: [Map],_loaded: false,_host: undefined,_value: 'Error, property was not loaded',_valueBeforeLoaded: undefined,_isObject: true,replace: 'Error, property was not loaded',before: 'Error, property was not loaded',after: 'Error, property was not loaded'}},_loaded: false,_host: undefined,_value: 'Error, property was not loaded',_valueBeforeLoaded: undefined,context: OfficeMockObject{_properties: Map(1){'document'=>[OfficeMockObject]},_loaded: false,_host: undefined,_value: 'Error, property was not loaded',_valueBeforeLoaded: undefined,_isObject: true,document: OfficeMockObject{_properties: Map(0){},_loaded: false,_host: undefined,_value: 'Error, property was not loaded',_valueBeforeLoaded: undefined,_isObject: true}},InsertLocation: OfficeMockObject{_properties: Map(3){'replace'=>[OfficeMockObject],'before'=>[OfficeMockObject],'after'=>[OfficeMockObject]},_loaded: false,_host: undefined,_value: 'Error, property was not loaded',_valueBeforeLoaded: undefined,_isObject: true,replace: 'Error, property was not loaded',before: 'Error, property was not loaded',after: 'Error, property was not loaded'},run: [AsyncFunction: run]}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello everyone,
My team and I are working on an Office Word Add-in which interacts with the document to insert text, images, tables, etc.
We've implemented a few helper functions to make those insertions with the Word JS API. Here's how one looks like:
However, it is not clear to us how to unit test those functions with the
office-addin-mock
. We know there is a section in the documentation about using that mock but it is very succinct and we don't know how to apply it to our use case.More specifically, it's not clear what should be added the the mock object data and how to test that after calling the
insertTagIntoDoc
function, the document contains the data as expected, in this case, either some text or an image. Also, we want to test that the inserted context replace the current selection in the document, and that the cursor is move to the end of the inserted content.Here's what we get when log the
wordMock
object in the console in our test:Thanks for your help.
Greg
Beta Was this translation helpful? Give feedback.
All reactions