Skip to content

Commit

Permalink
refactor(packages/sui-test): Fix prettier error
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosvillu committed Feb 7, 2024
1 parent 4e62131 commit 1f0dab0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 46 deletions.
4 changes: 1 addition & 3 deletions packages/sui-test/bin/karma/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ const config = {
...environmentVariables
}),
new webpack.DefinePlugin({
__MOCKS_API_PATH__: JSON.stringify(
process.env.MOCKS_API_PATH || process.env.PWD + '/mocks/routes'
),
__MOCKS_API_PATH__: JSON.stringify(process.env.MOCKS_API_PATH || process.env.PWD + '/mocks/routes'),
'process.env.SEED': JSON.stringify(process.env.SEED),
__BASE_DIR__: JSON.stringify(process.env.PWD),
PATTERN: JSON.stringify(process.env.PATTERN),
Expand Down
3 changes: 1 addition & 2 deletions packages/sui-test/src/MotherObject/FakeGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ let instanceRndID
export class RandomID {
static create() {
if (instanceRndID) return instanceRndID
const seed =
process.env.SEED || Math.ceil(Math.random() * Number.MAX_SAFE_INTEGER)
const seed = process.env.SEED || Math.ceil(Math.random() * Number.MAX_SAFE_INTEGER)
instanceRndID = new RandomID(seed)

console.log(`[RandomID.create] Faker created with seed: ${seed}`)
Expand Down
26 changes: 6 additions & 20 deletions packages/sui-test/src/MotherObject/Fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ export class MockFetcherManager {
}

static restore() {
if (!_instance)
return console.warn('Unable to restore a non-initialized MockFetcherManager')
if (!_instance) return console.warn('Unable to restore a non-initialized MockFetcherManager')
_instance.restore()
_instance = undefined
}

constructor(FetchFetcher, {InlineError}) {
this.FetchFetcher = FetchFetcher
this._originalsFetcher = this._methods.map(
method => this.FetchFetcher.prototype[method]
)
this._originalsFetcher = this._methods.map(method => this.FetchFetcher.prototype[method])
this._fakeRequests = {}
this._hasInlineErrors = InlineError
this._methods.forEach(method => {
Expand All @@ -31,27 +28,19 @@ export class MockFetcherManager {
addMock({url, method, error, mock, force}) {
const key = method.toUpperCase() + '::' + url
if (!force && this._fakeRequests[key])
throw new Error(
`[MockFetcherManager#addMock] forbidden overwrite the request ${key}`
)
throw new Error(`[MockFetcherManager#addMock] forbidden overwrite the request ${key}`)

this._fakeRequests[key] = [error, mock]
}

validate({url, method}) {
const key = method.toUpperCase() + '::' + url
if (this._fakeRequests[key])
throw new Error(
`[MockFetcherManager#validate] request ${key} don't consume`
)
if (this._fakeRequests[key]) throw new Error(`[MockFetcherManager#validate] request ${key} don't consume`)
}

restore() {
if (Object.keys(this._fakeRequests).length === 0) {
this._methods.forEach(
(method, index) =>
(this.FetchFetcher.prototype[method] = this._originalsFetcher[index])
)
this._methods.forEach((method, index) => (this.FetchFetcher.prototype[method] = this._originalsFetcher[index]))
} else {
throw new Error(`[MockFetcherManager#restore]
Unabled restore the FetchFetcher because there are request w/out been consume
Expand All @@ -70,10 +59,7 @@ export class MockFetcherManager {
if (self._fakeRequests[requestKey]) {
const [error, response] = self._fakeRequests[requestKey]
delete self._fakeRequests[requestKey]
if (!self._hasInlineErrors)
return error
? Promise.reject(error)
: Promise.resolve({data: response})
if (!self._hasInlineErrors) return error ? Promise.reject(error) : Promise.resolve({data: response})

return Promise.resolve([error, response])
} else {
Expand Down
19 changes: 4 additions & 15 deletions packages/sui-test/src/MotherObject/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ export const ModelMotherObjectFactory = Model =>
`[ModelMotherObject#validateToJSON] has failed! ${Model.constructor?.name} doesnt follow the rule: Model.create(instance.toJSON())`
)
diff.forEach(part => {
console.log(
`%c ${part.value}`,
`color: ${part.added ? 'green' : part.removed ? 'red' : 'grey'}`
)
console.log(`%c ${part.value}`, `color: ${part.added ? 'green' : part.removed ? 'red' : 'grey'}`)
})
throw err
}
Expand All @@ -72,26 +69,18 @@ export const ModelMotherObjectFactory = Model =>
try {
expect(expected).to.be.eql(json)
} catch (err) {
console.error(
'There are differences between the mother object model and your model'
)
console.error('There are differences between the mother object model and your model')
const diff = diffJson(expected, json)
diff.forEach(part => {
console.log(
`%c ${part.value}`,
`color: ${part.added ? 'green' : part.removed ? 'red' : 'grey'}`
)
console.log(`%c ${part.value}`, `color: ${part.added ? 'green' : part.removed ? 'red' : 'grey'}`)
})
throw err
}
return this
}

jsonModel() {
if (!this._model)
throw new Error(
`[ModelMotherObject#jsonModel] model is undefined model(${this._model})`
)
if (!this._model) throw new Error(`[ModelMotherObject#jsonModel] model is undefined model(${this._model})`)
return this._model.toLiterals?.() ?? this._model.toJSON()
}
}
8 changes: 2 additions & 6 deletions packages/sui-test/src/MotherObject/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export const RequestMotherObjectFactory = (FetchFetcher, {InlineError}) =>

success() {
if (this._error !== undefined)
throw new Error(
'[RequestMotherObject#success] Dont call to .success after call to .error method'
)
throw new Error('[RequestMotherObject#success] Dont call to .success after call to .error method')

this._mock = this.generate(this._faker)

Expand All @@ -55,9 +53,7 @@ export const RequestMotherObjectFactory = (FetchFetcher, {InlineError}) =>
)

if (this._mock !== undefined)
throw new Error(
'[RequestMotherObject#error] Dont call to .error after call to .success method'
)
throw new Error('[RequestMotherObject#error] Dont call to .error after call to .success method')

this._error = error
this._mockFetcherManager.addMock({
Expand Down

0 comments on commit 1f0dab0

Please sign in to comment.