defaults.test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { defaultConfig, defaultRobotsTxtTransformer, defaultSitemapTransformer, withDefaultConfig, } from '../defaults.js';
  3. describe('next-sitemap/defaults', () => {
  4. test('defaultConfig', () => {
  5. expect(defaultConfig).toStrictEqual({
  6. sourceDir: '.next',
  7. outDir: 'public',
  8. sitemapBaseFileName: 'sitemap',
  9. generateIndexSitemap: true,
  10. priority: 0.7,
  11. changefreq: 'daily',
  12. sitemapSize: 5000,
  13. autoLastmod: true,
  14. exclude: [],
  15. transform: defaultSitemapTransformer,
  16. robotsTxtOptions: {
  17. transformRobotsTxt: defaultRobotsTxtTransformer,
  18. policies: [
  19. {
  20. userAgent: '*',
  21. allow: '/',
  22. },
  23. ],
  24. additionalSitemaps: [],
  25. },
  26. });
  27. });
  28. test('withDefaultConfig', () => {
  29. const myConfig = withDefaultConfig({
  30. sourceDir: 'custom-source',
  31. generateRobotsTxt: true,
  32. generateIndexSitemap: true,
  33. sitemapSize: 50000,
  34. exclude: ['1', '2'],
  35. robotsTxtOptions: {
  36. policies: [],
  37. additionalSitemaps: [
  38. 'https://example.com/awesome-sitemap.xml',
  39. 'https://example.com/awesome-sitemap-2.xml',
  40. ],
  41. },
  42. });
  43. expect(myConfig).toStrictEqual({
  44. sourceDir: 'custom-source',
  45. outDir: 'public',
  46. sitemapBaseFileName: 'sitemap',
  47. generateIndexSitemap: true,
  48. priority: 0.7,
  49. changefreq: 'daily',
  50. sitemapSize: 50000,
  51. autoLastmod: true,
  52. generateRobotsTxt: true,
  53. exclude: ['1', '2'],
  54. transform: defaultSitemapTransformer,
  55. robotsTxtOptions: {
  56. transformRobotsTxt: defaultRobotsTxtTransformer,
  57. policies: [],
  58. additionalSitemaps: [
  59. 'https://example.com/awesome-sitemap.xml',
  60. 'https://example.com/awesome-sitemap-2.xml',
  61. ],
  62. },
  63. });
  64. });
  65. test('withDefaultConfig: Default transformation', async () => {
  66. const myConfig = withDefaultConfig({
  67. trailingSlash: false,
  68. sourceDir: 'custom-source',
  69. generateRobotsTxt: true,
  70. generateIndexSitemap: true,
  71. sitemapSize: 50000,
  72. exclude: ['1', '2'],
  73. priority: 0.6,
  74. changefreq: 'weekly',
  75. robotsTxtOptions: {
  76. policies: [],
  77. additionalSitemaps: [
  78. 'https://example.com/awesome-sitemap.xml',
  79. 'https://example.com/awesome-sitemap-2.xml',
  80. ],
  81. },
  82. });
  83. // Default transform
  84. await expect(myConfig.transform(myConfig, 'https://example.com')).resolves.toStrictEqual({
  85. loc: 'https://example.com',
  86. lastmod: expect.any(String),
  87. changefreq: 'weekly',
  88. priority: 0.6,
  89. alternateRefs: [],
  90. trailingSlash: myConfig.trailingSlash,
  91. });
  92. // Default transform with custom config override
  93. await expect(myConfig.transform({
  94. ...myConfig,
  95. trailingSlash: true,
  96. }, 'https://example.com')).resolves.toStrictEqual({
  97. loc: 'https://example.com',
  98. lastmod: expect.any(String),
  99. changefreq: 'weekly',
  100. priority: 0.6,
  101. alternateRefs: [],
  102. trailingSlash: true,
  103. });
  104. });
  105. test('withDefaultConfig: Custom transformation', async () => {
  106. const myConfig = withDefaultConfig({
  107. sourceDir: 'custom-source',
  108. generateRobotsTxt: true,
  109. sitemapSize: 50000,
  110. exclude: ['1', '2'],
  111. priority: 0.6,
  112. changefreq: 'weekly',
  113. transform: async () => {
  114. return {
  115. loc: 'something-else',
  116. lastmod: 'lastmod-cutom',
  117. };
  118. },
  119. robotsTxtOptions: {
  120. transformRobotsTxt: defaultRobotsTxtTransformer,
  121. policies: [],
  122. additionalSitemaps: [
  123. 'https://example.com/awesome-sitemap.xml',
  124. 'https://example.com/awesome-sitemap-2.xml',
  125. ],
  126. },
  127. });
  128. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  129. const value = await myConfig.transform(myConfig, 'https://example.com');
  130. expect(value).toStrictEqual({
  131. loc: 'something-else',
  132. lastmod: 'lastmod-cutom',
  133. });
  134. });
  135. });