defaults.test.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. "use strict";
  2. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  3. var __assign = (this && this.__assign) || function () {
  4. __assign = Object.assign || function(t) {
  5. for (var s, i = 1, n = arguments.length; i < n; i++) {
  6. s = arguments[i];
  7. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  8. t[p] = s[p];
  9. }
  10. return t;
  11. };
  12. return __assign.apply(this, arguments);
  13. };
  14. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  15. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  16. return new (P || (P = Promise))(function (resolve, reject) {
  17. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  18. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  19. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  20. step((generator = generator.apply(thisArg, _arguments || [])).next());
  21. });
  22. };
  23. var __generator = (this && this.__generator) || function (thisArg, body) {
  24. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  25. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  26. function verb(n) { return function (v) { return step([n, v]); }; }
  27. function step(op) {
  28. if (f) throw new TypeError("Generator is already executing.");
  29. while (_) try {
  30. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  31. if (y = 0, t) op = [op[0] & 2, t.value];
  32. switch (op[0]) {
  33. case 0: case 1: t = op; break;
  34. case 4: _.label++; return { value: op[1], done: false };
  35. case 5: _.label++; y = op[1]; op = [0]; continue;
  36. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  37. default:
  38. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  39. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  40. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  41. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  42. if (t[2]) _.ops.pop();
  43. _.trys.pop(); continue;
  44. }
  45. op = body.call(thisArg, _);
  46. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  47. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  48. }
  49. };
  50. exports.__esModule = true;
  51. var defaults_js_1 = require("../defaults.js");
  52. describe('next-sitemap/defaults', function () {
  53. test('defaultConfig', function () {
  54. expect(defaults_js_1.defaultConfig).toStrictEqual({
  55. sourceDir: '.next',
  56. outDir: 'public',
  57. sitemapBaseFileName: 'sitemap',
  58. generateIndexSitemap: true,
  59. priority: 0.7,
  60. changefreq: 'daily',
  61. sitemapSize: 5000,
  62. autoLastmod: true,
  63. exclude: [],
  64. transform: defaults_js_1.defaultSitemapTransformer,
  65. robotsTxtOptions: {
  66. transformRobotsTxt: defaults_js_1.defaultRobotsTxtTransformer,
  67. policies: [
  68. {
  69. userAgent: '*',
  70. allow: '/'
  71. },
  72. ],
  73. additionalSitemaps: []
  74. }
  75. });
  76. });
  77. test('withDefaultConfig', function () {
  78. var myConfig = (0, defaults_js_1.withDefaultConfig)({
  79. sourceDir: 'custom-source',
  80. generateRobotsTxt: true,
  81. generateIndexSitemap: true,
  82. sitemapSize: 50000,
  83. exclude: ['1', '2'],
  84. robotsTxtOptions: {
  85. policies: [],
  86. additionalSitemaps: [
  87. 'https://example.com/awesome-sitemap.xml',
  88. 'https://example.com/awesome-sitemap-2.xml',
  89. ]
  90. }
  91. });
  92. expect(myConfig).toStrictEqual({
  93. sourceDir: 'custom-source',
  94. outDir: 'public',
  95. sitemapBaseFileName: 'sitemap',
  96. generateIndexSitemap: true,
  97. priority: 0.7,
  98. changefreq: 'daily',
  99. sitemapSize: 50000,
  100. autoLastmod: true,
  101. generateRobotsTxt: true,
  102. exclude: ['1', '2'],
  103. transform: defaults_js_1.defaultSitemapTransformer,
  104. robotsTxtOptions: {
  105. transformRobotsTxt: defaults_js_1.defaultRobotsTxtTransformer,
  106. policies: [],
  107. additionalSitemaps: [
  108. 'https://example.com/awesome-sitemap.xml',
  109. 'https://example.com/awesome-sitemap-2.xml',
  110. ]
  111. }
  112. });
  113. });
  114. test('withDefaultConfig: Default transformation', function () { return __awaiter(void 0, void 0, void 0, function () {
  115. var myConfig;
  116. return __generator(this, function (_a) {
  117. switch (_a.label) {
  118. case 0:
  119. myConfig = (0, defaults_js_1.withDefaultConfig)({
  120. trailingSlash: false,
  121. sourceDir: 'custom-source',
  122. generateRobotsTxt: true,
  123. generateIndexSitemap: true,
  124. sitemapSize: 50000,
  125. exclude: ['1', '2'],
  126. priority: 0.6,
  127. changefreq: 'weekly',
  128. robotsTxtOptions: {
  129. policies: [],
  130. additionalSitemaps: [
  131. 'https://example.com/awesome-sitemap.xml',
  132. 'https://example.com/awesome-sitemap-2.xml',
  133. ]
  134. }
  135. });
  136. // Default transform
  137. return [4 /*yield*/, expect(myConfig.transform(myConfig, 'https://example.com')).resolves.toStrictEqual({
  138. loc: 'https://example.com',
  139. lastmod: expect.any(String),
  140. changefreq: 'weekly',
  141. priority: 0.6,
  142. alternateRefs: [],
  143. trailingSlash: myConfig.trailingSlash
  144. })
  145. // Default transform with custom config override
  146. ];
  147. case 1:
  148. // Default transform
  149. _a.sent();
  150. // Default transform with custom config override
  151. return [4 /*yield*/, expect(myConfig.transform(__assign(__assign({}, myConfig), { trailingSlash: true }), 'https://example.com')).resolves.toStrictEqual({
  152. loc: 'https://example.com',
  153. lastmod: expect.any(String),
  154. changefreq: 'weekly',
  155. priority: 0.6,
  156. alternateRefs: [],
  157. trailingSlash: true
  158. })];
  159. case 2:
  160. // Default transform with custom config override
  161. _a.sent();
  162. return [2 /*return*/];
  163. }
  164. });
  165. }); });
  166. test('withDefaultConfig: Custom transformation', function () { return __awaiter(void 0, void 0, void 0, function () {
  167. var myConfig, value;
  168. return __generator(this, function (_a) {
  169. switch (_a.label) {
  170. case 0:
  171. myConfig = (0, defaults_js_1.withDefaultConfig)({
  172. sourceDir: 'custom-source',
  173. generateRobotsTxt: true,
  174. sitemapSize: 50000,
  175. exclude: ['1', '2'],
  176. priority: 0.6,
  177. changefreq: 'weekly',
  178. transform: function () { return __awaiter(void 0, void 0, void 0, function () {
  179. return __generator(this, function (_a) {
  180. return [2 /*return*/, {
  181. loc: 'something-else',
  182. lastmod: 'lastmod-cutom'
  183. }];
  184. });
  185. }); },
  186. robotsTxtOptions: {
  187. transformRobotsTxt: defaults_js_1.defaultRobotsTxtTransformer,
  188. policies: [],
  189. additionalSitemaps: [
  190. 'https://example.com/awesome-sitemap.xml',
  191. 'https://example.com/awesome-sitemap-2.xml',
  192. ]
  193. }
  194. });
  195. return [4 /*yield*/, myConfig.transform(myConfig, 'https://example.com')];
  196. case 1:
  197. value = _a.sent();
  198. expect(value).toStrictEqual({
  199. loc: 'something-else',
  200. lastmod: 'lastmod-cutom'
  201. });
  202. return [2 /*return*/];
  203. }
  204. });
  205. }); });
  206. });