Skip to content

add depth check to oneNoteItemUtils #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/oneNoteDataStructures/oneNoteItemUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { Polyfills } from '../polyfills';

Polyfills.find();

export type notebookOrSectionGroup = Notebook | SectionGroup;
export type SectionPathElement = notebookOrSectionGroup | Section;

export class OneNoteItemUtils {
/**
* Given the id of the OneNoteItem, and a notebook or sectionGroup list, returns
Expand Down Expand Up @@ -98,6 +101,38 @@ export class OneNoteItemUtils {

return ancestry;
}

/**
* Finds the maximum depth of notebooks list, including sections
*/
static getDepthOfNotebooks(notebooks: Notebook[]): number {
if (!notebooks || notebooks.length === 0) {
return 0;
}
return notebooks.map((notebook) => this.getDepthOfParent(notebook)).reduce((d1, d2) => Math.max(d1, d2));
}

/**
* Finds the maximum depth of SectionGroup or Notebook,
* includes the number of sections below it
*/
private static getDepthOfParent(parent: notebookOrSectionGroup ): number {
if (!parent) {
return 0;
}

let containsAtLeastOneSection = parent.sections && parent.sections.length > 0;
let maxDepth = containsAtLeastOneSection ? 1 : 0;

if (parent.sectionGroups) {
for (let i = 0; i < parent.sectionGroups.length; i++) {
maxDepth = Math.max(this.getDepthOfParent(parent.sectionGroups[i]), maxDepth);
}
}

// Include the parent itself
return maxDepth + 1;
}
}

export * from './oneNoteApiResponseTransformer';
139 changes: 138 additions & 1 deletion test/oneNoteDataStructures/oneNoteItemUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,93 @@
import { Notebook } from '../../src/oneNoteDataStructures/notebook';
import { OneNoteItem } from '../../src/oneNoteDataStructures/oneNoteItem';
import { OneNoteItemUtils } from '../../src/oneNoteDataStructures/oneNoteItemUtils';
import { SectionGroup } from '../../src/oneNoteDataStructures/sectionGroup';
import { OneNoteItemUtils, SectionPathElement } from '../../src/oneNoteDataStructures/oneNoteItemUtils';
import { Section } from '../../src/oneNoteDataStructures/section';

describe('OneNoteItemUtils', () => {
const notebook1: Notebook = {
Copy link
Contributor

@jogonzal jogonzal Apr 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a number of entities here that your test use throughout several tests here. I'll simplify a bit:

const notebook1: Notebook = {
... sections: [] ...
}
it('test1', () => {
        notebook1.push(section1);
		const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1]);
		expect(depthOfNotebooks).toEqual(2);
	});
it('test2', () => {
        const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1]);
		expect(depthOfNotebooks).toEqual(1);
	});

If both tests run in the same persisted browser instance, then the order in which the tests run affects whether they pass or not, because they both use notebook1. A better way to build this could be:

function createNewNotebook(){
  return new {
    ... sections: [] ...
  }
}
it('test1', () => {
        var notebookMock = createNotebookMock();
        notebook1.push(section1);
		const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebookMock]);
		expect(depthOfNotebooks).toEqual(2);
	});
it('test2', () => {
        const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([createNotebookMock()]);
		expect(depthOfNotebooks).toEqual(1);
	});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also have a parameter to specify the notebook name (if you wish to) in createNewNotebook().

parent: undefined,
id: 'notebook1',
name: 'Notebook1',
expanded: true,
sectionGroups: [],
sections: [],
apiUrl: '',
webUrl: '',
};

const notebook2: Notebook = {
parent: undefined,
id: 'notebook2',
name: 'Notebook2',
expanded: false,
sectionGroups: [],
sections: [],
apiUrl: '',
webUrl: '',
};

const expandedNotebook: Notebook = {
parent: undefined,
id: 'notebook2',
name: 'Notebook2',
expanded: false,
sectionGroups: [],
sections: [],
apiUrl: '',
webUrl: '',
};

const section1: Section = {
parent: undefined,
id: 'section1',
name: 'Section1',
expanded: false,
pages: undefined,
apiUrl: '',
};

const section2: Section = {
parent: undefined,
id: 'section1',
name: 'Section1',
expanded: false,
pages: undefined,
apiUrl: '',
};

const sectionGroup: SectionGroup = {
parent: notebook2,
id: 'sg1',
name: 'sg1',
expanded: false,
sectionGroups: [],
sections: [],
apiUrl: ''
};

const sectionGroupWithTwoSections: SectionGroup = {
parent: notebook2,
id: 'sg2',
name: 'Sg2',
expanded: false,
sectionGroups: [],
sections: [],
apiUrl: ''
};
sectionGroupWithTwoSections.sections.push(section1, section2);

const stackedSectionGroup: SectionGroup = {
parent: notebook2,
id: 'sg3',
name: 'sg3',
expanded: false,
sectionGroups: [],
sections: [],
apiUrl: ''
};
stackedSectionGroup.sectionGroups.push(sectionGroup);

it('find should return the item that matches the predicate if it is found early in the hierarchy', () => {
const notebook: Notebook = {
parent: undefined,
Expand Down Expand Up @@ -217,4 +301,57 @@ describe('OneNoteItemUtils', () => {
const ancestry = OneNoteItemUtils.getAncestry(item);
expect(ancestry).toEqual([grandparent, parent, item]);
});

it('getDepthOfNotebooks should return a max depth of 1 if there is one notebook with no other children', () => {
const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1]);
expect(depthOfNotebooks).toEqual(1);
});

it('getDepthOfNotebooks should return a max depth of 1 if there are two notebooks with no other children', () => {
const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]);
expect(depthOfNotebooks).toEqual(1);
});

it('getDepthOfNotebooks returns a max depth of 2 when there are two notebooks where one has sections', () => {
section1.parent = notebook1;
notebook1.sections.push(section1);

const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]);
expect(depthOfNotebooks).toEqual(2);
});

it('getDepthOfNotebook should return a max depth of 2 when there are two notebooks where one has an empty section group', () => {
notebook1.sectionGroups.push(sectionGroup);

const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]);
expect(depthOfNotebooks).toEqual(2);
});

it('getDepthOfNotebooks returns a max depth of 3 when there are two notebooks where one has a section group with a section', () => {
sectionGroup.sections.push(section1);
notebook1.sectionGroups.push(sectionGroup);

const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]);
expect(depthOfNotebooks).toEqual(3);
});

it('getDepthOfNotebooks returns a max depth of three when there are two notebooks where one has a section group with multiple sections', () => {
notebook1.sectionGroups.push(sectionGroupWithTwoSections);

const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]);
expect(depthOfNotebooks).toEqual(3);
});

it('getDepthOfNotebooks returns the correct depth when there are two notebooks with a more complicated tree structure', () => {
notebook1.sectionGroups.push(sectionGroupWithTwoSections);
notebook2.sectionGroups.push(stackedSectionGroup);

const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, expandedNotebook]);
expect(depthOfNotebooks).toEqual(3);
});

it('getDepthOfNotebooks returns a max depth of 0 if it is given an empty array of Notebooks', () => {
expect(OneNoteItemUtils.getDepthOfNotebooks([])).toEqual(0);
});

});