-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added unit tests for react-dnd-html5-backend (#1283)
- Loading branch information
1 parent
ef319ee
commit aa584be
Showing
7 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
packages/react-dnd-html5-backend/src/__tests__/BrowserDetector.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { isFirefox } from '../BrowserDetector' | ||
|
||
describe('BrowserDetector', () => { | ||
it('should detect firefox', () => { | ||
expect(isFirefox()).toEqual(false) | ||
}) | ||
}) |
48 changes: 48 additions & 0 deletions
48
packages/react-dnd-html5-backend/src/__tests__/EnterLeaveCounter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import EnterLeaveCounter from '../EnterLeaveCounter' | ||
|
||
describe('EnterLeaveCounter', () => { | ||
const parentDiv = document.createElement('div') | ||
parentDiv.setAttribute('id', 'parent') | ||
const childDiv = document.createElement('div') | ||
childDiv.setAttribute('id', 'child') | ||
parentDiv.appendChild(childDiv) | ||
|
||
const isNodeInDocument = () => { | ||
return true | ||
} | ||
it('should check if the element is entered', () => { | ||
const enterLeaveCounter: EnterLeaveCounter = new EnterLeaveCounter( | ||
isNodeInDocument, | ||
) | ||
const hasEntered = enterLeaveCounter.enter(parentDiv) | ||
expect(hasEntered).toEqual(true) | ||
}) | ||
|
||
it('should not falsely check child elements as new element entered', () => { | ||
const enterLeaveCounter: EnterLeaveCounter = new EnterLeaveCounter( | ||
isNodeInDocument, | ||
) | ||
enterLeaveCounter.enter(parentDiv) | ||
const hasEntered = enterLeaveCounter.enter(childDiv) | ||
expect(hasEntered).toEqual(false) | ||
}) | ||
|
||
it('should check if leave element was entered', () => { | ||
const enterLeaveCounter: EnterLeaveCounter = new EnterLeaveCounter( | ||
isNodeInDocument, | ||
) | ||
enterLeaveCounter.enter(parentDiv) | ||
const testElem = document.createElement('p') | ||
const hasLeft = enterLeaveCounter.leave(testElem) | ||
expect(hasLeft).toEqual(false) | ||
}) | ||
|
||
it('should leave element if it is entered', () => { | ||
const enterLeaveCounter: EnterLeaveCounter = new EnterLeaveCounter( | ||
isNodeInDocument, | ||
) | ||
enterLeaveCounter.enter(parentDiv) | ||
const hasLeft = enterLeaveCounter.leave(parentDiv) | ||
expect(hasLeft).toEqual(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/react-dnd-html5-backend/src/__tests__/OffsetUtils.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
getNodeClientOffset, | ||
getEventClientOffset, | ||
getDragPreviewOffset, | ||
} from '../OffsetUtils' | ||
|
||
describe('OffsetUtils', () => { | ||
describe('getNodeClientOffset', () => { | ||
it('should get node client offset for element node', () => { | ||
const el = document.createElement('div') | ||
expect(getNodeClientOffset(el)).toEqual({ | ||
x: 0, | ||
y: 0, | ||
}) | ||
}) | ||
|
||
it('should return get parent node client offset if element type is not ELEMENT NODE', () => { | ||
const el = document.createElement('p') | ||
el.textContent = 'Text inside paragraph' | ||
expect(getNodeClientOffset(el.firstChild)).toEqual({ | ||
x: 0, | ||
y: 0, | ||
}) | ||
}) | ||
|
||
it('should return null if parent node is not of Node.ELEMENT_NODE', () => { | ||
const el = document.createComment('this is comment node of type 3') | ||
expect(getNodeClientOffset(el)).toEqual(null) | ||
}) | ||
}) | ||
|
||
describe('getEventClientOffset', () => { | ||
it('should return client offset', () => { | ||
const el = document.createEvent('MouseEvent') | ||
expect(getEventClientOffset(el)).toEqual({ | ||
x: 0, | ||
y: 0, | ||
}) | ||
}) | ||
}) | ||
|
||
describe('getDragPreviewOffset', () => { | ||
it('should get offset of drag preview component', () => { | ||
const sourceNode = document.createElement('div') | ||
const dragPreview = document.createElement('div') | ||
const clientOffset = { | ||
x: 0, | ||
y: 0, | ||
} | ||
const anchorPoint = { | ||
anchorX: 0, | ||
anchorY: 0, | ||
} | ||
const offsetPoint = { | ||
offsetX: 0, | ||
offsetY: 0, | ||
} | ||
expect( | ||
getDragPreviewOffset( | ||
sourceNode, | ||
dragPreview, | ||
clientOffset, | ||
anchorPoint, | ||
offsetPoint, | ||
), | ||
).toEqual({ | ||
x: 0, | ||
y: 0, | ||
}) | ||
}) | ||
}) | ||
}) |
8 changes: 8 additions & 0 deletions
8
packages/react-dnd-html5-backend/src/__tests__/getEmptyImage.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import getEmptyImage from '../getEmptyImage' | ||
|
||
describe('Get Empty Image', () => { | ||
it('should return image', () => { | ||
const image = getEmptyImage() | ||
expect(image.getAttribute('src')).toBeDefined() | ||
}) | ||
}) |
15 changes: 15 additions & 0 deletions
15
packages/react-dnd-html5-backend/src/__tests__/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import createHTML5Backend, { HTML5BackendContext } from '../index' | ||
import { DragDropManager } from 'dnd-core' | ||
|
||
describe('index', () => { | ||
it('should return HTML5 backend', () => { | ||
const mockManager: DragDropManager<HTML5BackendContext> = { | ||
getActions: () => null, | ||
getMonitor: () => null, | ||
getRegistry: () => null, | ||
getContext: () => ({}), | ||
} as any | ||
const backend = createHTML5Backend(mockManager) | ||
expect(backend).toBeDefined() | ||
}) | ||
}) |
21 changes: 21 additions & 0 deletions
21
packages/react-dnd-html5-backend/src/__tests__/matchesType.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import matchesType from '../matchesType' | ||
|
||
describe('matchesType', () => { | ||
it('should match type for single source and single target', () => { | ||
const target = 'a' | ||
const source = 'a' | ||
expect(matchesType(target, source)).toBeTruthy() | ||
}) | ||
|
||
it('should match type for single source and multiple targets', () => { | ||
const targets = ['a', 'b', 'c'] | ||
const source = 'a' | ||
expect(matchesType(targets, source)).toBeTruthy() | ||
}) | ||
|
||
it('should match type when source and target are null', () => { | ||
const target = null | ||
const source = null | ||
expect(matchesType(target, source)).toBeTruthy() | ||
}) | ||
}) |