-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:support all array native method and add all test case
- Loading branch information
fancyzhong
committed
Jun 26, 2021
1 parent
95b056b
commit 3d04930
Showing
43 changed files
with
500 additions
and
94 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
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
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,69 @@ | ||
import { createDraft, finishDraft, produce } from '../src/index'; | ||
|
||
export const produceTip = (testDescribe: string) => `${testDescribe} (with produce)`; | ||
|
||
export const createDraftTip = (testDescribe: string) => `${testDescribe} (with createDraft, finishDraft)`; | ||
|
||
/** | ||
* allow noop pass any params | ||
*/ | ||
export function noop(...args: any[]) { | ||
if (args === []) console?.log(args); | ||
} | ||
|
||
/** | ||
* common getArrBase handler | ||
* @returns | ||
*/ | ||
export function getArrBase() { | ||
return [1, 2, 3]; | ||
} | ||
|
||
/** | ||
* common compare handler | ||
* new and base should be equal | ||
*/ | ||
export function shouldBeEqual(arrNew, arrBase) { | ||
expect(arrNew === arrBase).toBeTruthy(); | ||
} | ||
/** | ||
* common compare handler | ||
* new and base should be not equal | ||
*/ | ||
export function shouldBeNotEqual(arrNew, arrBase) { | ||
expect(arrNew !== arrBase).toBeTruthy(); | ||
} | ||
|
||
/** | ||
* | ||
* @param testSuitDesc | ||
* @param testCaseDesc | ||
* @param getArrBase | ||
* @param operateDraft | ||
* @param executeAssertLogic | ||
*/ | ||
export function runTestSuit( | ||
testSuitDesc: string, | ||
testCaseDesc: string, | ||
getArrBase: () => any[], | ||
operateDraft: (arrDraft: any[], arrBase: any[]) => void, | ||
executeAssertLogic: (arrNew: any[], arrBase: any[]) => void, | ||
) { | ||
describe(testSuitDesc, () => { | ||
test(createDraftTip(testCaseDesc), () => { | ||
const arrBase = getArrBase(); | ||
const arrDraft = createDraft(arrBase); | ||
operateDraft(arrDraft, arrBase); | ||
const arrNew = finishDraft(arrDraft); | ||
executeAssertLogic(arrNew, arrBase); | ||
}); | ||
|
||
test(produceTip(testCaseDesc), () => { | ||
const arrBase = getArrBase(); | ||
const arrNew = produce(arrBase, arrDraft => { | ||
operateDraft(arrDraft, arrBase); | ||
}); | ||
executeAssertLogic(arrNew, arrBase); | ||
}) | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
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,9 @@ | ||
import { runTestSuit, getArrBase, shouldBeEqual } from '../_util'; | ||
|
||
function changeDraft(arrDraft) { | ||
const arrTmp = arrDraft.concat([4, 5]); | ||
expect(arrTmp).toMatchObject([1, 2, 3, 4, 5]); | ||
} | ||
|
||
runTestSuit('test concat', 'concat', getArrBase, changeDraft, shouldBeEqual); | ||
|
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,19 @@ | ||
import { runTestSuit } from '../_util'; | ||
|
||
function getArrBase() { | ||
return [100, 200, 1, 2, 3, 4]; | ||
} | ||
|
||
function changeDraft(arrDraft: any[]) { | ||
const arrTmp = arrDraft.copyWithin(3, 1, 3); | ||
expect(arrTmp).toMatchObject([100, 200, 1, 200, 1, 4]); | ||
} | ||
|
||
function compare(arrNew, arrBase) { | ||
expect(arrNew !== arrBase).toBeTruthy(); | ||
expect(arrNew).toMatchObject([100, 200, 1, 200, 1, 4]); | ||
expect(arrBase).toMatchObject([100, 200, 1, 2, 3, 4]); | ||
} | ||
|
||
runTestSuit('test copyWithin', 'copyWithin', getArrBase, changeDraft, compare); | ||
|
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 { runTestSuit, getArrBase, shouldBeNotEqual } from '../_util'; | ||
|
||
function changeDraft(arrDraft) { | ||
delete arrDraft['0']; | ||
} | ||
|
||
runTestSuit('test delete', 'delete', getArrBase, changeDraft, shouldBeNotEqual); |
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,9 @@ | ||
import { runTestSuit, getArrBase, shouldBeEqual } from '../_util'; | ||
|
||
function changeDraft(arrDraft: any[]) { | ||
// return Array Iterator {} | ||
const entries = arrDraft.entries(); | ||
expect(entries).toBeTruthy(); | ||
} | ||
|
||
runTestSuit('test entries', 'entries', getArrBase, changeDraft, shouldBeEqual); |
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 { runTestSuit, getArrBase, shouldBeEqual } from '../_util'; | ||
|
||
function changeDraft(arrDraft) { | ||
const result = arrDraft.every(item => item !== 0); | ||
expect(result).toBeTruthy(); | ||
} | ||
|
||
runTestSuit('test every', 'every', getArrBase, changeDraft, shouldBeEqual); |
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,17 @@ | ||
import { runTestSuit } from '../_util'; | ||
|
||
function getArrBase() { | ||
return [1, 2, 3, 4, 5, 6]; | ||
} | ||
|
||
function changeDraft(arrDraft: any[]) { | ||
const result = arrDraft.fill(10, 2, 5); | ||
expect(result).toMatchObject([1, 2, 10, 10, 10, 6]); | ||
} | ||
|
||
function compare(arrNew, arrBase) { | ||
expect(arrNew).toMatchObject([1, 2, 10, 10, 10, 6]); | ||
expect(arrBase).toMatchObject([1, 2, 3, 4, 5, 6]); | ||
} | ||
|
||
runTestSuit('test fill', 'fill', getArrBase, changeDraft, compare); |
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 { runTestSuit, getArrBase, shouldBeEqual } from '../_util'; | ||
|
||
function changeDraft(arrDraft) { | ||
const result = arrDraft.filter(item => item > 1); | ||
expect(result).toMatchObject([2, 3]); | ||
} | ||
|
||
runTestSuit('test filter', 'filter', getArrBase, changeDraft, shouldBeEqual); |
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 { runTestSuit, getArrBase, shouldBeEqual } from '../_util'; | ||
|
||
function changeDraft(arrDraft) { | ||
const result = arrDraft.find(item => item === 1); | ||
expect(result === 1).toBeTruthy(); | ||
} | ||
|
||
runTestSuit('test find', 'find', getArrBase, changeDraft, shouldBeEqual); |
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 { runTestSuit, getArrBase, shouldBeEqual } from '../_util'; | ||
|
||
function changeDraft(arrDraft) { | ||
const result = arrDraft.findIndex(item => item === 1); | ||
expect(result === 0).toBeTruthy(); | ||
} | ||
|
||
runTestSuit('test findIndex', 'findIndex', getArrBase, changeDraft, shouldBeEqual); |
Oops, something went wrong.