This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test): ups mock refactoring
Refactored the UPS nock mock to make it more modular, maintainable and extensible
- Loading branch information
Showing
15 changed files
with
631 additions
and
401 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,73 @@ | ||
import {PushApplication} from '../../../src/applications'; | ||
import {Guid} from 'guid-typescript'; | ||
import {Variant} from '../../../src/variants'; | ||
|
||
export class UPSEngineMock { | ||
private data: PushApplication[] = []; | ||
|
||
createApplication(newAppDef: PushApplication) { | ||
const newApp: PushApplication = {...(newAppDef as {})} as PushApplication; | ||
|
||
newApp.masterSecret = Guid.raw(); | ||
newApp.pushApplicationID = newApp.pushApplicationID || Guid.raw(); | ||
newApp.id = Guid.raw(); | ||
newApp.developer = newApp.developer || 'admin'; | ||
this.data.push(newApp); | ||
return newApp; | ||
} | ||
|
||
getApplications(id?: string, page = 1, itemPerPage = 10) { | ||
if (!id) { | ||
const firstIndex = itemPerPage * (page - 1); | ||
const endIndex = firstIndex + itemPerPage; | ||
|
||
return this.data.slice(firstIndex, endIndex); | ||
} else { | ||
return this.data.find(item => item.pushApplicationID === id); | ||
} | ||
} | ||
|
||
deleteApplication(id: string) { | ||
this.data = this.data.filter(item => item.pushApplicationID !== id); | ||
} | ||
|
||
// Variants | ||
createVariant(appId: string, variantDef: Variant) { | ||
const newVariant: Variant = {...(variantDef as {})} as Variant; | ||
newVariant.variantID = newVariant.variantID || Guid.raw(); | ||
newVariant.developer = newVariant.developer || 'admin'; | ||
newVariant.secret = Guid.raw(); | ||
newVariant.id = Guid.raw(); | ||
|
||
const app = this.data.find(item => item.pushApplicationID === appId)!; | ||
app.variants = app.variants || []; | ||
app.variants.push(newVariant); | ||
|
||
return newVariant; | ||
} | ||
|
||
getVariants(appId: string, type: string) { | ||
const app = this.getApplications(appId) as PushApplication; | ||
if (!app) { | ||
return null; | ||
} | ||
|
||
return app.variants ? app.variants.filter(variant => variant.type === type) : []; | ||
} | ||
|
||
deleteVariant(appId: string, type: string, variantId: string) { | ||
const app = this.getApplications(appId) as PushApplication; | ||
if (!app) { | ||
return null; | ||
} | ||
|
||
const variant = | ||
app.variants && app.variants.find(variant => variant.type === type && variant.variantID === variantId); | ||
if (variant) { | ||
app.variants = app.variants?.filter(variant => variant.type !== type && variant.variantID !== variantId); | ||
} | ||
return variant; | ||
} | ||
|
||
reset = () => (this.data = []); | ||
} |
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,2 @@ | ||
export {utils} from './utils'; | ||
export {UPSMock} from './rest/UPSMock'; |
Oops, something went wrong.