Skip to content

Commit

Permalink
Number shading & distribution duplicates correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
kpinakula committed Jan 21, 2025
1 parent 2304e3a commit b664998
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ import userEvent from "@testing-library/user-event";
import { screen } from '@testing-library/vue';
import HotWaterDistribution from './index.vue';

const state: HotWaterDistributionData = {
name: 'Pipework Kitchen Sink',
length: 3,
location: 'internal',
internalDiameter: 30,
externalDiameter: 33,
insulationThickness: 10,
insulationThermalConductivity: 35,
pipeContents: 'air',
surfaceReflectivity: 'yes',
}

describe('Hot water distribution', () => {
const store = useEcaasStore();
const user = userEvent.setup();

const distribution1: HotWaterDistributionData = {
name: 'Pipework Kitchen Sink',
length: 3,
location: 'internal',
internalDiameter: 30,
externalDiameter: 33,
insulationThickness: 10,
insulationThermalConductivity: 35,
pipeContents: 'air',
surfaceReflectivity: 'yes',
}

const distribution2: HotWaterDistributionData = {
name: 'Pipework Kitchen',
length: 4,
location: 'internal',
}

afterEach(() => {
store.$reset();
});
Expand All @@ -28,7 +34,7 @@ describe('Hot water distribution', () => {
dwellingDetails: {
hotWaterDistribution: {
data: {
distributions: [state]
distributions: [distribution1]
}
}
}
Expand All @@ -49,7 +55,7 @@ describe('Hot water distribution', () => {
dwellingDetails: {
hotWaterDistribution: {
data: {
distributions: [state]
distributions: [distribution1, distribution2]
}
}
}
Expand All @@ -58,8 +64,12 @@ describe('Hot water distribution', () => {
await renderSuspended(HotWaterDistribution);

await user.click(screen.getByTestId('customListItemDuplicate_0'));
await user.click(screen.getByTestId('customListItemDuplicate_0'));
await user.click(screen.getByTestId('customListItemDuplicate_1'));

expect(screen.queryAllByTestId('customListItem').length).toBe(2);
expect(screen.queryAllByTestId('customListItem').length).toBe(5);
expect(screen.getByText('Pipework Kitchen Sink (1)')).toBeDefined();
expect(screen.getByText('Pipework Kitchen Sink (2)')).toBeDefined();
expect(screen.getByText('Pipework Kitchen (1)')).toBeDefined();
});
});
6 changes: 4 additions & 2 deletions pages/dwelling-details/hot-water-distribution/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
function handleDuplicate(index: number) {
const distribution = distributions[index];
const duplicates = distributions.filter(d => d.name.startsWith(distribution.name));
const name_pattern = distribution.name.replaceAll("(", "\\(").replaceAll(")", "\\)");
const duplicate_name_pattern = new RegExp(String.raw`^${name_pattern}( \([0-9]+\))?$`);
const duplicates = distributions.filter(d => d.name.match(duplicate_name_pattern));
if (distribution) {
store.$patch((state) => {
Expand All @@ -47,4 +49,4 @@
v-on:duplicate="handleDuplicate"
/>
<GovButton href="/dwelling-details" secondary>Return to overview</GovButton>
</template>
</template>
4 changes: 3 additions & 1 deletion pages/dwelling-details/shading/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ function handleRemove(index: number) {
function handleDuplicate(index: number) {
const shading = shadingObjects[index];
const duplicates = shadingObjects.filter(s => s.name.startsWith(shading.name));
const name_pattern = shading.name.replaceAll("(", "\\(").replaceAll(")", "\\)");
const duplicate_pattern = new RegExp(String.raw`^${name_pattern}( \([0-9]+\))?$`);
const duplicates = shadingObjects.filter(s => s.name.match(duplicate_pattern));
if (shading) {
store.$patch((state) => {
Expand Down
17 changes: 15 additions & 2 deletions pages/dwelling-details/shading/shading.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ describe('shading', () => {
name: 'Apple Tree'
};

const shading3: ShadingObject = {
...shading1,
name: 'Cherry Tree out front'
};

afterEach(() => {
store.$reset();
});
Expand Down Expand Up @@ -73,7 +78,7 @@ describe('shading', () => {
dwellingDetails: {
shading: {
data: {
shadingObjects: [shading1]
shadingObjects: [shading1, shading3]
}
}
}
Expand All @@ -82,8 +87,16 @@ describe('shading', () => {
await renderSuspended(Shading);

await userEvent.click(screen.getByTestId('customListItemDuplicate_0'));
await userEvent.click(screen.getByTestId('customListItemDuplicate_0'));
await userEvent.click(screen.getByTestId('customListItemDuplicate_2'));
await userEvent.click(screen.getByTestId('customListItemDuplicate_2'));

expect(screen.queryAllByTestId('customListItem').length).toBe(2);
expect(screen.queryAllByTestId('customListItem').length).toBe(6);
expect(screen.getByText('Cherry Tree')).toBeDefined();
expect(screen.getByText('Cherry Tree (1)')).toBeDefined();
expect(screen.getByText('Cherry Tree (2)')).toBeDefined();
expect(screen.getByText('Cherry Tree (1) (1)')).toBeDefined();
expect(screen.getByText('Cherry Tree (1) (2)')).toBeDefined();
})
});

0 comments on commit b664998

Please sign in to comment.