From 0cb21ad13950ec7b2f8afb0e47af0da660fae402 Mon Sep 17 00:00:00 2001 From: Marc Font <36164126+Marketen@users.noreply.github.com> Date: Mon, 11 Nov 2024 09:10:30 +0100 Subject: [PATCH] add deletedns unit test (#2057) * add deletedns unit test * fix removeDns() * use double quotes * fix editor with lint --------- Co-authored-by: pablomendezroyo --- packages/dockerCompose/src/editor.ts | 12 ++--- .../test/unit/composeDeleteDns.test.ts | 47 +++++++++++++++++++ 2 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 packages/dockerCompose/test/unit/composeDeleteDns.test.ts diff --git a/packages/dockerCompose/src/editor.ts b/packages/dockerCompose/src/editor.ts index c2e46dfdd..e2e94441b 100644 --- a/packages/dockerCompose/src/editor.ts +++ b/packages/dockerCompose/src/editor.ts @@ -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 { diff --git a/packages/dockerCompose/test/unit/composeDeleteDns.test.ts b/packages/dockerCompose/test/unit/composeDeleteDns.test.ts new file mode 100644 index 000000000..fd92c73e4 --- /dev/null +++ b/packages/dockerCompose/test/unit/composeDeleteDns.test.ts @@ -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"); + }); + }); +});