Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kesha-antonov committed Feb 16, 2024
1 parent 66e2a6a commit 4262bc4
Show file tree
Hide file tree
Showing 5 changed files with 1,060 additions and 1,918 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ An absolute path to the app's documents directory. It is recommended that you us

## TODO

- [ ] Write better examples - current kinda old and not very good
- [ ] Check tests are up to date
- [ ] Write better examples - current kinda old and shallow
- [ ] Write better API for downloads - current kinda boilerplate
- [ ] Add more tests

## Authors

Expand Down
31 changes: 12 additions & 19 deletions __mocks__/RNBackgroundDownloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,43 @@ NativeModules.RNBackgroundDownloader = {
TaskSuspended: 1,
TaskCanceling: 2,
TaskCompleted: 3,
initDownloader: jest.fn(),
checkForExistingDownloads: jest.fn().mockImplementation(() => {
foundDownloads = [
{
id: 'taskRunning',
state: NativeModules.RNBackgroundDownloader.TaskRunning,
percent: 0.5,
bytesWritten: 50,
totalBytes: 100
bytesDownloaded: 50,
bytesTotal: 100
},
{
id: 'taskPaused',
state: NativeModules.RNBackgroundDownloader.TaskSuspended,
percent: 0.7,
bytesWritten: 70,
totalBytes: 100
bytesDownloaded: 70,
bytesTotal: 100
},
{
id: 'taskCancelled',
percent: 0.9,
state: NativeModules.RNBackgroundDownloader.TaskCanceling,
bytesWritten: 90,
totalBytes: 100
bytesDownloaded: 90,
bytesTotal: 100
},
{
id: 'taskCompletedExplicit',
state: NativeModules.RNBackgroundDownloader.TaskCompleted,
percent: 1,
bytesWritten: 100,
totalBytes: 100
bytesDownloaded: 100,
bytesTotal: 100
},
{
id: 'taskCompletedImplicit',
state: NativeModules.RNBackgroundDownloader.TaskCompleted,
percent: 1,
bytesWritten: 100,
totalBytes: 100
bytesDownloaded: 100,
bytesTotal: 100
},
{
id: 'taskFailed',
state: NativeModules.RNBackgroundDownloader.TaskCompleted,
percent: 0.9,
bytesWritten: 90,
totalBytes: 100
bytesDownloaded: 90,
bytesTotal: 100
}
]
return Promise.resolve(foundDownloads);
Expand Down
30 changes: 8 additions & 22 deletions __tests__/mainTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ test('progress event', () => {
id: 'testProgress',
url: 'test',
destination: 'test',
}).progress((percent, bytesWritten, totalBytes) => {
expect(percent).toBeCloseTo(0.7)
expect(bytesWritten).toBe(100)
expect(totalBytes).toBe(200)
}).progress(({ bytesDownloaded, bytesTotal }) => {
expect(bytesDownloaded).toBe(100)
expect(bytesTotal).toBe(200)
resolve()
})
nativeEmitter.emit('downloadProgress', [{
id: 'testProgress',
percent: 0.7,
written: 100,
total: 200,
bytesDownloaded: 100,
bytesTotal: 200,
}])
})
})
Expand All @@ -81,14 +79,16 @@ test('fail event', () => {
id: 'testFail',
url: 'test',
destination: 'test',
}).error(error => {
}).error(({ error, errorCode }) => {
expect(error).toBeInstanceOf(Error)
expect(errorCode).toBe(-1)
expect(failDT.state).toBe('FAILED')
resolve()
})
nativeEmitter.emit('downloadFailed', {
id: 'testFail',
error: new Error('test'),
errorCode: -1,
})
})
})
Expand Down Expand Up @@ -129,20 +129,6 @@ test('stop', () => {
expect(RNBackgroundDownloaderNative.stopTask).toHaveBeenCalled()
})

test('initDownloader', () => {
jest.mock('react-native/Libraries/Utilities/Platform', () => {
const Platform = jest.requireActual(
'react-native/Libraries/Utilities/Platform'
)
Platform.OS = 'android'
return Platform
})

const res = RNBackgroundDownloader.initDownloader()
expect(RNBackgroundDownloaderNative.initDownloader).toHaveBeenCalled()
expect(res).toBe(undefined)
})

test('checkForExistingDownloads', () => {
return RNBackgroundDownloader.checkForExistingDownloads()
.then(foundDownloads => {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
"setupFiles": [
"./__mocks__/RNBackgroundDownloader.js",
"./node_modules/react-native/Libraries/EventEmitter/__mocks__/NativeEventEmitter.js"
],
"roots": [
"./__tests__"
]
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 4262bc4

Please sign in to comment.