Skip to content

Commit

Permalink
Merge pull request #3674 from openstax/handle_book_units
Browse files Browse the repository at this point in the history
Handle books with units in the book section search
  • Loading branch information
nathanstitt authored Jul 7, 2021
2 parents 2f5ace1 + e01b652 commit af15a3b
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 6 deletions.
122 changes: 122 additions & 0 deletions exercises/specs/components/search/book-sections.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import BookSections from '../../../src/components/search/book-sections';
import Books from '../../../src/models/books'
import Search from '../../../src/models/search'

describe('Search book sections', function() {

describe('partsToOptions', function() {

it('ignores units', () => {
const search = new Search();
Books.ensureLoaded = jest.fn()
const props = { search: search };
const bookSections = new BookSections(props);
expect(bookSections.partsToOptions(
[
{
id: 'db36053c-5281-42f4-90ec-afcc21ab28c3@18',
title: '<span data-type="" itemprop="" class="os-text">Preface</span>',
slug: 'preface',
},
{
id: '[email protected]',
title: '<span class="os-number"><span class="os-part-text">Unit </span>1</span><span class="os-divider"> </span><span data-type="" itemprop="" class="os-text">The Chemistry of Life</span>',
contents: [
{
id: '[email protected]',
title: '<span class="os-number"><span class="os-part-text">Chapter </span>1</span><span class="os-divider"> </span><span data-type="" itemprop="" class="os-text">The Study of Life</span>',
contents: [
{
id: '2230ab90-3137-4dcb-b6bd-72630222948c@10',
title: '<span data-type="" itemprop="" class="os-text">Introduction</span>',
slug: '1-introduction',
},
{
id: 'ce6cfde4-1ac3-480f-aa0f-4777111f9e23@23',
title: '<span class="os-number">1.1</span><span class="os-divider"> </span><span data-type="" itemprop="" class="os-text">The Science of Biology</span>',
slug: '1-1-the-science-of-biology',
},
{
id: '07dbec85-8530-4911-b863-5d73b5d7e211@18',
title: '<span class="os-number">1.2</span><span class="os-divider"> </span><span data-type="" itemprop="" class="os-text">Themes and Concepts of Biology</span>',
slug: '1-2-themes-and-concepts-of-biology',
},
{
id: '[email protected]',
title: '<span class="os-text">Key Terms</span>',
slug: '1-key-terms',
},
{
id: '[email protected]',
title: '<span class="os-text">Chapter Summary</span>',
slug: '1-chapter-summary',
},
{
id: '[email protected]',
title: '<span class="os-text">Visual Connection Questions</span>',
slug: '1-visual-connection-questions',
},
{
id: '[email protected]',
title: '<span class="os-text">Review Questions</span>',
slug: '1-review-questions',
},
{
id: '[email protected]',
title: '<span class="os-text">Critical Thinking Questions</span>',
slug: '1-critical-thinking-questions',
},
],
slug: '1-the-study-of-life',
},
],
},
]
)).toEqual([
{
label: '<span data-type="" itemprop="" class="os-text">Preface</span>',
value: 'db36053c-5281-42f4-90ec-afcc21ab28c3',
},
{
label: '<span class="os-number"><span class="os-part-text">Chapter </span>1</span><span class="os-divider"> </span><span data-type="" itemprop="" class="os-text">The Study of Life</span>',
options: [
{
label: '<span data-type="" itemprop="" class="os-text">Introduction</span>',
value: '2230ab90-3137-4dcb-b6bd-72630222948c',
},
{
label: '<span class="os-number">1.1</span><span class="os-divider"> </span><span data-type="" itemprop="" class="os-text">The Science of Biology</span>',
value: 'ce6cfde4-1ac3-480f-aa0f-4777111f9e23',
},
{
label: '<span class="os-number">1.2</span><span class="os-divider"> </span><span data-type="" itemprop="" class="os-text">Themes and Concepts of Biology</span>',
value: '07dbec85-8530-4911-b863-5d73b5d7e211',
},
{
label: '<span class="os-text">Key Terms</span>',
value: '761608a3-5b67-533e-a199-c03f0b88a95b',
},
{
label: '<span class="os-text">Chapter Summary</span>',
value: '0d3ce2c6-8ebe-5f8e-b3d4-1c91cfca2a91',
},
{
label: '<span class="os-text">Visual Connection Questions</span>',
value: '1c6c7f98-e992-536c-9769-cf24cefe451d',
},
{
label: '<span class="os-text">Review Questions</span>',
value: '3eec7061-a32e-529d-a594-b9f53aed0331',
},
{
label: '<span class="os-text">Critical Thinking Questions</span>',
value: 'bbe32ad1-f92d-5f62-b911-4fde45c04a58',
},
],
},
]);
});

});

});
17 changes: 14 additions & 3 deletions exercises/src/components/search/book-sections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,28 @@ class BookSections extends React.Component<BookSectionsProps> {
partsToOptions(parts?: BookPart[]): OptionType[] {
if (!parts) return []

return parts.map(part => {
return parts.flatMap(part => {
const option:OptionType = { label: part.title }

if (part.contents) {
option.options = this.partsToOptions(part.contents)
// Chapter or Unit
const nestedOptions = this.partsToOptions(part.contents)

if (part.contents.some(content => content.contents)) {
// Unit
return nestedOptions
}
else {
// Chapter
option.options = nestedOptions
}
}
else {
// Page
option.value = part.id.split('@', 1)[0]
}

return option
return [option]
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ exports[`Course Settings, course details matches snapshot 1`] = `
name="startDate"
readOnly={true}
type="text"
value="Mar 28, 2021"
value="Oct 15, 2020"
/>
</div>
<label
Expand All @@ -331,7 +331,7 @@ exports[`Course Settings, course details matches snapshot 1`] = `
name="endDate"
readOnly={true}
type="text"
value="Jul 28, 2021"
value="Feb 15, 2021"
/>
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion tutor/specs/screens/course-settings/details.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from 'react'
import CourseDetails from '../../../src/screens/course-settings/course-details';
import { Factory } from '../../helpers'
import { TimeMock, Factory } from '../../helpers'
import type { Course } from '../../../src/models'
import { mount } from 'enzyme'

describe('Course Settings, course details', () => {
let props: any;
let course: Course;

TimeMock.setTo('2021-01-15T12:00:00.000Z');

beforeEach(() => {
course = Factory.course()
props = {
Expand Down

0 comments on commit af15a3b

Please sign in to comment.