diff --git a/country-explorer/src/components/CountryCard.test.tsx b/country-explorer/src/components/CountryCard.test.tsx index 234814f..316f272 100644 --- a/country-explorer/src/components/CountryCard.test.tsx +++ b/country-explorer/src/components/CountryCard.test.tsx @@ -72,3 +72,28 @@ describe('CountryCard', () => { expect(screen.getAllByText('Not available')).toHaveLength(6); }); }); + +describe('CountryCard snapshot', () => { + it('matches the snapshot for a country with complete data', () => { + const { container } = render( + , + ); + + // CountryCard links its heading to the card with an id from useId(). + // That id is a React implementation detail, not something derived from + // the country data, and it isn't guaranteed to stay the same (it can + // shift with the React version or render order). Replace it with a + // fixed placeholder so the snapshot only captures data that should + // actually stay the same, and doesn't fail for unrelated reasons. + const heading = container.querySelector('h3'); + const article = container.querySelector('article'); + heading?.setAttribute('id', 'country-card-heading'); + article?.setAttribute('aria-labelledby', 'country-card-heading'); + + // To intentionally update this snapshot after a real change to + // CountryCard's markup, run `npm test -- -u` (or `npx vitest -u`) and + // review the diff in __snapshots__/CountryCard.test.tsx.snap before + // committing it. + expect(container.firstChild).toMatchSnapshot(); + }); +}); diff --git a/country-explorer/src/components/__snapshots__/CountryCard.test.tsx.snap b/country-explorer/src/components/__snapshots__/CountryCard.test.tsx.snap new file mode 100644 index 0000000..0e65294 --- /dev/null +++ b/country-explorer/src/components/__snapshots__/CountryCard.test.tsx.snap @@ -0,0 +1,97 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`CountryCard snapshot > matches the snapshot for a country with complete data 1`] = ` +
+ Flag of Norway +
+
+

+ Norway +

+ +
+
+
+
+ Capital +
+
+ Oslo +
+
+
+
+ Region +
+
+ Europe +
+
+
+
+ Population +
+
+ 5,379,475 +
+
+
+
+ Area +
+
+ 323,802 km² +
+
+
+
+ Languages +
+
+ Norwegian, Sámi +
+
+
+
+ Currency +
+
+ Norwegian krone (NOK) +
+
+
+
+
+`;