Skip to content

Commit

Permalink
fix: fixed some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed Aug 19, 2024
1 parent a73c08f commit 3964bdc
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 11 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/navigation/InternalNavigator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ describe(`Given loaded book`, () => {

describe("Given invalid positive navigation spine item", () => {
it(`should fallback to length - 1`, async () => {
const { internalNavigator, userNavigator, spineItemsManagerMock } =
const { internalNavigator, userNavigator, spineItemsManagerMock, spine } =
createNavigator()
const navigations: InternalNavigationEntry[] = []

spineItemsManagerMock.load(generateItems(100, 2))

spine.layout()

const sub = internalNavigator.navigated$.subscribe((navigation) => {
navigations.push(navigation)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ describe(`Given a backward navigation to a new item`, () => {
// items of 2 pages
spineItemsManager.load(generateItems(100, 2))

spine.layout()

const position = restoreNavigationForControlledPageTurnMode({
navigation: {
position: {
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/navigation/tests/SpineItemsManagerMock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Subject } from "rxjs"
import { BehaviorSubject, NEVER, of, Subject } from "rxjs"

export type Item = {
left: number
Expand All @@ -15,6 +15,8 @@ export class SpineItemsManagerMock {

public layout$ = new Subject()

public items$ = new BehaviorSubject<Item[]>([])

load(
items: {
left: number
Expand All @@ -29,7 +31,15 @@ export class SpineItemsManagerMock {
...item,
getElementDimensions: () => ({ width: item.width, height: item.height }),
isUsingVerticalWriting: () => false,
adjustPositionOfElement: () => {},
layout: () => ({ width: item.width, height: item.height }),
$: {
contentLayout$: NEVER,
loaded$: of(true),
},
}))

this.items$.next(this.items)
}

get(id: number) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/navigation/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ export const createNavigator = () => {
userNavigator.locker.isLocked$,
)

return { internalNavigator, userNavigator, context, spineItemsManagerMock }
return { internalNavigator, userNavigator, context, spineItemsManagerMock, spine }
}
12 changes: 8 additions & 4 deletions packages/core/src/spine/locator/getItemVisibilityForPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ export const getItemVisibilityForPosition = ({
context: Context
}) => {
const viewportLeft = viewportPosition.x
const viewportRight =
viewportPosition.x + (context.state.visibleAreaRect.width - 1)
const viewportRight = Math.max(
viewportPosition.x + (context.state.visibleAreaRect.width - 1),
0,
)
const viewportTop = viewportPosition.y
const viewportBottom =
viewportPosition.y + (context.state.visibleAreaRect.height - 1)
const viewportBottom = Math.max(
viewportPosition.y + (context.state.visibleAreaRect.height - 1),
0,
)
// const viewportWidth = context.state.visibleAreaRect.width

const visibleWidthOfItem =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,19 @@ describe("Given single page items and no spread", () => {
settings,
)

context.update({
visibleAreaRect: {
height: 100,
width: 100,
x: 0,
y: 0,
},
})

spineItemsManager.load(singlePageItems)

spineLayout.layout()

const { beginIndex, endIndex } =
getVisibleSpineItemsFromPosition({
context: context,
Expand Down Expand Up @@ -78,8 +89,19 @@ describe("Given single page items and no spread", () => {
settings,
)

context.update({
visibleAreaRect: {
height: 100,
width: 100,
x: 0,
y: 0,
},
})

spineItemsManager.load(singlePageItems)

spineLayout.layout()

const { beginIndex, endIndex } =
getVisibleSpineItemsFromPosition({
context: context,
Expand Down Expand Up @@ -107,8 +129,19 @@ describe("Given single page items and no spread", () => {
settings,
)

context.update({
visibleAreaRect: {
height: 100,
width: 100,
x: 0,
y: 0,
},
})

spineItemsManager.load(singlePageItems)

spineLayout.layout()

const { beginIndex, endIndex } =
getVisibleSpineItemsFromPosition({
context: context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export const getVisibleSpineItemsFromPosition = ({
}):
| {
beginIndex: number
// beginPosition: ViewportPosition
endIndex: number
// endPosition: ViewportPosition
}
| undefined => {
const fallbackSpineItem =
Expand Down Expand Up @@ -70,8 +68,6 @@ export const getVisibleSpineItemsFromPosition = ({

return {
beginIndex: beginItemIndex ?? 0,
// beginPosition: position,
endIndex: endItemIndex ?? 0,
// endPosition: position,
}
}

0 comments on commit 3964bdc

Please sign in to comment.