Skip to content

Commit

Permalink
Add tests for contract partial functions
Browse files Browse the repository at this point in the history
Signed-off-by: Vipul Gupta (@vipulgupta2048) <[email protected]>
  • Loading branch information
vipulgupta2048 committed Feb 15, 2023
1 parent 424fc46 commit fd8c90e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/models/device-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const traversingCompile = (
initial: Contract,
path: string[],
): Contract => {
let interpolated: Contract = { ...initial }
let interpolated: Contract = { ...initial };
for (const partialKey of Object.keys(partials)) {
const current = partials[partialKey];
if (Array.isArray(current)) {
Expand All @@ -52,7 +52,7 @@ const traversingCompile = (
}
}
return interpolated;
}
};

const interpolatedPartials = (contract: Contract): Contract => {
if (contract.partials) {
Expand Down Expand Up @@ -417,7 +417,9 @@ const getDeviceTypeModel = function (deps: InjectedDependenciesParam) {
$select: 'contract',
});
if (!contract) {
throw new Error(`Could not find contract for device type ${deviceTypeSlug}`);
throw new Error(
`Could not find contract for device type ${deviceTypeSlug}`,
);
}
return interpolatedPartials(contract);
},
Expand Down Expand Up @@ -459,7 +461,10 @@ const getDeviceTypeModel = function (deps: InjectedDependenciesParam) {
}
const installMethod = calculateInstallMethod(contract);
const interpolatedDeviceType = interpolatedPartials(contract);
const interpolatedHostOS = interpolatedPartials({...cloneDeep(BalenaOS), ...interpolatedDeviceType});
const interpolatedHostOS = interpolatedPartials({
...cloneDeep(BalenaOS),
...interpolatedDeviceType,
});

return interpolatedHostOS.partials?.[installMethod];
},
Expand Down
2 changes: 1 addition & 1 deletion lib/types/device-type-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface DeviceType {
slug: string;
name: string;
aliases: string[];

arch: string;
state?: string;
community?: boolean;
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/models/device-type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ describe('Device Type model', function () {
);
expect(partials).to.be.an('object');
expect(Object.keys(partials)).to.not.have.length(0);
expect(partials)
.to.have.property('partials')
.to.have.property('bootDevice');
expect(partials?.partials?.bootDevice[0]).to.equal(
'Connect power to the Raspberry Pi 2',
);
});
});

Expand All @@ -110,6 +116,14 @@ describe('Device Type model', function () {
);
expect(partials).to.be.an('Array');
expect(partials).to.not.have.length(0);
expect(partials).to.eql([
'Insert the sdcard to the host machine.',
'Write the balenaOS file you downloaded to the sdcard. We recommend using <a href="http://www.etcher.io/">Etcher</a>.',
'Wait for writing of balenaOS to complete.',
'Remove the sdcard from the host machine.',
'Insert the freshly flashed sdcard into the Raspberry Pi 2.',
'Connect power to the Raspberry Pi 2 to boot the device.',
]);
});
});

Expand Down

0 comments on commit fd8c90e

Please sign in to comment.