Skip to content

Commit

Permalink
fix: config.replace tests (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias authored Jan 13, 2023
1 parent f6d0ae9 commit 020bf54
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
10 changes: 1 addition & 9 deletions test/interface-tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,7 @@ function executeTests (commonFactory) {

tests.bootstrap(commonFactory)

tests.config(commonFactory, {
skip: [
// config.replace
{
name: 'replace',
reason: 'FIXME https://github.com/ipfs/js-kubo-rpc-client/issues/97'
}
]
})
tests.config(commonFactory)

tests.dag(commonFactory)

Expand Down
39 changes: 32 additions & 7 deletions test/interface-tests/src/config/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,49 @@ export function testReplace (factory, options) {

after(async function () { return await factory.clean() })

const config = {
Addresses: {
API: ''
it('should replace the whole config', async () => {
const config = {
Addresses: {
API: '/ip4/1.2.3.4/tcp/54321/'
}
}
}

it('should replace the whole config', async () => {
await ipfs.config.replace(config)

const _config = await ipfs.config.getAll()
expect(_config).to.deep.equal(config)
expect(cleanConfig(_config)).to.deep.equal(config)
})

it('should replace to empty config', async () => {
await ipfs.config.replace({})

const _config = await ipfs.config.getAll()
expect(_config).to.deep.equal({})
expect(cleanConfig(_config)).to.deep.equal({})
})
})
}

// cleanConfig removes all null properties of the configuration. When replacing
// a configuration, Kubo always fills missing properties with null values.
function cleanConfig (config) {
if (Array.isArray(config)) {
return config.filter(e => Boolean(e))
}

return Object.keys(config).reduce((acc, key) => {
if (!config[key]) {
return acc
}

if (typeof config[key] === 'object') {
const o = cleanConfig(config[key])
if (o && Object.keys(o).length) {
acc[key] = o
}
} else if (config[key]) {
acc[key] = config[key]
}

return acc
}, {})
}

0 comments on commit 020bf54

Please sign in to comment.