Skip to content

Commit

Permalink
add deletedns unit test (#2057)
Browse files Browse the repository at this point in the history
* add deletedns unit test

* fix removeDns()

* use double quotes

* fix editor with lint

---------

Co-authored-by: pablomendezroyo <[email protected]>
  • Loading branch information
Marketen and pablomendezroyo authored Nov 11, 2024
1 parent d5d60bf commit 0cb21ad
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
12 changes: 5 additions & 7 deletions packages/dockerCompose/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,13 @@ export class ComposeServiceEditor {
}

/**
* Remove the property, dont append undefined to the yaml
* Remove the property directly from the service.
*/
removeDns(): void {
this.edit((service) => {
if (!service.dns) return service;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { dns, ...rest } = service;
return rest;
});
const service = this.get();
if ("dns" in service) {
delete service.dns;
}
}

removeNetworkAliases(networkName: string, aliasesToRemove: string[], serviceNetwork: ComposeServiceNetwork): void {
Expand Down
47 changes: 47 additions & 0 deletions packages/dockerCompose/test/unit/composeDeleteDns.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import "mocha";
import { expect } from "chai";
import { ComposeEditor } from "../../src/index.js";
import { Compose } from "@dappnode/types";

describe("ComposeServiceEditor", function () {
describe("removeDns()", function () {
it("should remove the dns field from the service", function () {
// Create a mock compose object
const mockCompose: Compose = {
version: "3",
services: {
myservice: {
image: "myimage",
dns: "8.8.8.8",
environment: []
}
}
};

// Create a ComposeEditor instance with the mock compose
const composeEditor = new ComposeEditor(mockCompose);

// Get the service editor for 'myservice'
const serviceEditor = composeEditor.services()["myservice"];

// Ensure dns field is present before removal
expect(serviceEditor.get().dns).to.deep.equal("8.8.8.8");

// Call removeDns()
serviceEditor.removeDns();

// Get the updated service
const updatedService = serviceEditor.get();

// Verify that the dns field is removed
expect(updatedService.dns).to.be.undefined;

// Output the compose and check that dns is not present
const outputCompose = composeEditor.output();
expect(outputCompose.services["myservice"].dns).to.be.undefined;

// Ensure other fields remain unchanged
expect(outputCompose.services["myservice"].image).to.equal("myimage");
});
});
});

0 comments on commit 0cb21ad

Please sign in to comment.