-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cb 4633 unit tests for utils (#2357)
* CB-4633 blobToBase64 unit tests * СB-4633 importLazyComponent tests * CB-4633 OrderedMap unit tests * CB-4633 removed test time limit from blobToBase64 test * CB-4633 adds to ordered map unit tests for edge cases * CB-4633 importLazy moved to core-blocks * CB-4633 adds update-ts-references --------- Co-authored-by: s.teleshev <[email protected]>
- Loading branch information
1 parent
7eba02a
commit d96b9e9
Showing
32 changed files
with
298 additions
and
36 deletions.
There are no files selected for viewing
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
28 changes: 28 additions & 0 deletions
28
webapp/packages/core-blocks/src/__custom__mocks__/ErrorBoundaryMock.tsx
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,28 @@ | ||
import React from 'react'; | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
type State = { | ||
hasError: boolean; | ||
error: any | null; | ||
}; | ||
export default class ErrorBoundary extends React.Component<Props, State> { | ||
constructor(props: any) { | ||
super(props); | ||
this.state = { hasError: false, error: null }; | ||
} | ||
|
||
static getDerivedStateFromError(error: any) { | ||
return { hasError: true, error }; | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return <h1>{this.state.error.message}</h1>; | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
webapp/packages/core-blocks/src/importLazyComponent.test.tsx
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,43 @@ | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import React, { Suspense } from 'react'; | ||
|
||
import ErrorBoundary from './__custom__mocks__/ErrorBoundaryMock'; | ||
import { importLazyComponent } from './importLazyComponent'; | ||
|
||
describe('importLazyComponent', () => { | ||
const fallback = 'Loading...'; | ||
|
||
it('should render the lazy component', async () => { | ||
const loadedText = 'Lazy Component'; | ||
const mockComponent: React.FC<any> = () => <div>{loadedText}</div>; | ||
const componentImporter = jest.fn(() => Promise.resolve(mockComponent)); | ||
const LazyComponent = importLazyComponent(componentImporter); | ||
render( | ||
<Suspense fallback={fallback}> | ||
<LazyComponent /> | ||
</Suspense>, | ||
); | ||
|
||
expect(screen.getByText(fallback)).toBeTruthy(); | ||
await waitFor(() => expect(screen.getByText(loadedText)).toBeTruthy()); | ||
expect(componentImporter).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should render the error boundary if rejects with an error', async () => { | ||
const errorText = 'Error'; | ||
const componentImporter = jest.fn(() => Promise.reject(new Error(errorText))); | ||
const LazyComponent = importLazyComponent(componentImporter as any); | ||
|
||
render( | ||
<ErrorBoundary> | ||
<Suspense fallback={fallback}> | ||
<LazyComponent /> | ||
</Suspense> | ||
</ErrorBoundary>, | ||
); | ||
|
||
expect(screen.getByText(fallback)).toBeTruthy(); | ||
await waitFor(() => expect(screen.getByText(errorText)).toBeTruthy()); | ||
expect(componentImporter).toHaveBeenCalled(); | ||
}); | ||
}); |
File renamed without changes.
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
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
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,170 @@ | ||
import { OrderedMap } from './OrderedMap'; | ||
|
||
describe('OrderedMap', () => { | ||
it('should add and get items', () => { | ||
const map = new OrderedMap<number, string>(); | ||
map.add(1, 'one'); | ||
map.add(2, 'two'); | ||
map.add(3, 'three'); | ||
map.add(Infinity, 'infinity'); | ||
map.add(NaN, 'nan'); | ||
|
||
expect(map.get(1)).toBe('one'); | ||
expect(map.get(2)).toBe('two'); | ||
expect(map.get(3)).toBe('three'); | ||
expect(map.get(Infinity)).toBe('infinity'); | ||
expect(map.get(NaN)).toBe('nan'); | ||
}); | ||
|
||
it('should not get not exist item and return undefined', () => { | ||
const map = new OrderedMap<number, string>(); | ||
map.add(1, 'one'); | ||
map.add(2, 'two'); | ||
map.add(3, 'three'); | ||
|
||
expect(map.get(4)).toBeUndefined(); | ||
expect(map.get(Infinity)).toBeUndefined(); | ||
expect(map.get(NaN)).toBeUndefined(); | ||
}); | ||
|
||
it('should addValue and get items', () => { | ||
const toKey = (v: string) => v.length; | ||
const map = new OrderedMap<number, string>(toKey); | ||
map.addValue('one'); | ||
map.addValue('twoo'); | ||
map.addValue('three'); | ||
|
||
expect(map.get(3)).toBe('one'); | ||
expect(map.get(4)).toBe('twoo'); | ||
expect(map.get(5)).toBe('three'); | ||
}); | ||
|
||
it('should not override with addValue if it already exists', () => { | ||
const toKey = (v: string) => v.length; | ||
const map = new OrderedMap<number, string>(toKey); | ||
map.addValue('one'); | ||
map.addValue('twoo'); | ||
map.addValue('three'); | ||
map.addValue('four'); | ||
|
||
expect(map.get(4)).toBe('twoo'); | ||
}); | ||
|
||
it('should not addValue if no key fn', () => { | ||
const map = new OrderedMap<number, string>(); | ||
expect(() => map.addValue('one')).toThrow(); | ||
}); | ||
|
||
it('should be ordered', () => { | ||
const map = new OrderedMap<number, string>(); | ||
map.add(3, 'three'); | ||
map.add(1, 'one'); | ||
map.add(2, 'two'); | ||
|
||
expect(map.keys).toEqual([3, 1, 2]); | ||
expect(map.values).toEqual(['three', 'one', 'two']); | ||
}); | ||
|
||
it('should has items', () => { | ||
const map = new OrderedMap<number, string>(); | ||
map.add(1, 'one'); | ||
map.add(2, 'two'); | ||
map.add(3, 'three'); | ||
map.add(Infinity, 'infinity'); | ||
map.add(NaN, 'nan'); | ||
|
||
expect(map.has(1)).toBeTruthy(); | ||
expect(map.has(2)).toBeTruthy(); | ||
expect(map.has(3)).toBeTruthy(); | ||
expect(map.has(Infinity)).toBeTruthy(); | ||
expect(map.has(NaN)).toBeTruthy(); | ||
}); | ||
|
||
it('should not override items', () => { | ||
const map = new OrderedMap<number, string>(); | ||
map.add(1, 'one'); | ||
map.add(1, 'two'); | ||
map.add(Infinity, 'infinity'); | ||
map.add(Infinity, 'infinity2'); | ||
map.add(NaN, 'nan'); | ||
map.add(NaN, 'nan2'); | ||
|
||
expect(map.get(1)).toBe('one'); | ||
expect(map.get(Infinity)).toBe('infinity'); | ||
expect(map.get(NaN)).toBe('nan'); | ||
}); | ||
|
||
it('should remove items', () => { | ||
const map = new OrderedMap<number, string>(); | ||
map.add(1, 'one'); | ||
map.add(2, 'two'); | ||
map.add(3, 'three'); | ||
|
||
map.remove(2); | ||
|
||
expect(map.get(2)).toBeUndefined(); | ||
expect(map.keys).toEqual([1, 3]); | ||
expect(map.values).toEqual(['one', 'three']); | ||
}); | ||
|
||
it('should not have non-existing items after removal', () => { | ||
const map = new OrderedMap<number, string>(); | ||
map.add(1, 'one'); | ||
map.add(2, 'two'); | ||
map.add(3, 'three'); | ||
|
||
expect(map.has(4)).toBeFalsy(); | ||
expect(map.get(4)).toBeUndefined(); | ||
map.remove(4); | ||
expect(map.has(4)).toBeFalsy(); | ||
expect(map.get(4)).toBeUndefined(); | ||
}); | ||
|
||
it('should remove all items', () => { | ||
const map = new OrderedMap<number, string>(); | ||
map.add(1, 'one'); | ||
map.add(2, 'two'); | ||
map.add(3, 'three'); | ||
|
||
map.removeAll(); | ||
|
||
expect(map.keys).toEqual([]); | ||
expect(map.values).toEqual([]); | ||
}); | ||
|
||
it('should throw bulk update items if no toKey fn', () => { | ||
const map = new OrderedMap<number, string>(); | ||
|
||
expect(() => map.bulkUpdate(['one', 'two', 'three'])).toThrow(); | ||
}); | ||
|
||
it('should bulk update items', () => { | ||
const toKey = (v: string) => v.length; | ||
const map = new OrderedMap<number, string>(toKey); | ||
map.bulkUpdate(['o', 'tw', 'thr']); | ||
|
||
expect(map.keys).toEqual([1, 2, 3]); | ||
expect(map.values).toEqual(['o', 'tw', 'thr']); | ||
}); | ||
|
||
it('should bulk rewrite items', () => { | ||
const toKey = (v: string) => v.length; | ||
const map = new OrderedMap<number, string>(toKey); | ||
map.bulkUpdate(['o', 'tw', 'thr']); | ||
map.bulkRewrite(['one', 'twoo', 'three']); | ||
|
||
expect(map.keys).toEqual([3, 4, 5]); | ||
expect(map.values).toEqual(['one', 'twoo', 'three']); | ||
}); | ||
|
||
it('should sort items', () => { | ||
const map = new OrderedMap<number, string>(); | ||
map.add(3, 'c'); | ||
map.add(1, 'a'); | ||
map.add(2, 'b'); | ||
|
||
map.sort((a, b) => (a > b ? 1 : -1)); | ||
|
||
expect(map.keys).toEqual([1, 2, 3]); | ||
}); | ||
}); |
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,34 @@ | ||
import { blobToBase64 } from './blobToBase64'; | ||
|
||
describe('blobToBase64', () => { | ||
it('converts blob to base64', async () => { | ||
const blob = new Blob(['test'], { type: 'text/plain' }); | ||
const base64 = await blobToBase64(blob); | ||
|
||
expect(base64).toBe('data:text/plain;base64,dGVzdA=='); | ||
}); | ||
|
||
it('converts blob to base64 with slice', async () => { | ||
const blob = new Blob(['this is a test with longer text']); | ||
const base64 = await blobToBase64(blob, 20); | ||
|
||
expect(base64).toBe('data:application/octet-stream;base64,dGhpcyBpcyBhIHRlc3Qgd2l0aCA='); | ||
}); | ||
|
||
it('calls readAsDataURL', async () => { | ||
const readAsDataURL = jest.fn(blob => Promise.resolve(blob)); | ||
Object.defineProperty(global, 'FileReader', { | ||
writable: true, | ||
value: jest.fn().mockImplementation(() => ({ | ||
readAsDataURL, | ||
})), | ||
}); | ||
jest.useFakeTimers(); | ||
|
||
const blob = new Blob(['test'], { type: 'text/plain' }); | ||
|
||
blobToBase64(blob); | ||
|
||
expect(readAsDataURL).toHaveBeenCalledWith(blob); | ||
}); | ||
}); |
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
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
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
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
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
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
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
Oops, something went wrong.