Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions app/serializers/crate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ApplicationSerializer from './application';

const SKIP_NULL_FIELDS = new Set(['categories', 'keywords']);
const SKIP_NULL_FIELDS = new Set(['categories', 'keywords', 'max_stable_version']);

export default class CrateSerializer extends ApplicationSerializer {
isNewSerializerAPI = true;
Expand All @@ -17,15 +17,33 @@ export default class CrateSerializer extends ApplicationSerializer {
// We don't want existing relationships overwritten by results with null values.
// See: https://github.com/rust-lang/crates.io/issues/10711
if (payload.crates) {
payload.crates = payload.crates.map(crate => {
for (const rel of SKIP_NULL_FIELDS) {
if (crate[rel] === null) {
delete crate[rel];
}
}
return crate;
});
payload.crates.forEach(crate => removeNullFields(crate));
}

return super.normalizeQueryResponse(...arguments);
}

normalizeQueryRecordResponse(_store, _modelClass, payload) {
if (payload.crate) {
removeNullFields(payload.crate);
}

return super.normalizeQueryResponse(...arguments);
}
}

function removeNullFields(crate) {
for (let rel of SKIP_NULL_FIELDS) {
if (crate[rel] === null) {
delete crate[rel];
}
}

if (crate.max_version == '0.0.0') {
delete crate.max_version;
}

if (crate.newest_version == '0.0.0') {
delete crate.newest_version;
}
}
57 changes: 57 additions & 0 deletions e2e/bugs/11772.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { test, expect } from '@/e2e/helper';

test.describe('Bug #11772', { tag: '@bugs' }, () => {
function prepare(msw: any) {
// Create a crate that will appear in "New Crates" section
let newCrate = msw.db.crate.create({ name: 'test-crate' });
msw.db.version.create({ crate: newCrate, num: '1.2.3' });
}

test('crate versions should remain correct after navigating back from crate details', async ({ page, msw }) => {
prepare(msw);

// Visit homepage
await page.goto('/');
await expect(page).toHaveURL('/');

// Verify initial correct version displays
await expect(page.locator('[data-test-new-crates] [data-test-crate-link]')).toContainText('test-crate v1.2.3');
await expect(page.locator('[data-test-just-updated] [data-test-crate-link]')).toContainText('test-crate v1.2.3');

// Click on a crate to navigate to its details page
await page.click('[data-test-new-crates] [data-test-crate-link]');

// Verify we're on the crate details page
await expect(page).toHaveURL('/crates/test-crate');

await page.goto('/'); // Re-visit to simulate the back navigation

// Versions should still be displayed correctly, not v0.0.0
await expect(page.locator('[data-test-new-crates] [data-test-crate-link]')).toContainText('test-crate v1.2.3');
await expect(page.locator('[data-test-just-updated] [data-test-crate-link]')).toContainText('test-crate v1.2.3');
});

test('crates with actual v0.0.0 versions should display correctly', async ({ page, msw }) => {
// Create a crate with an actual v0.0.0 version
let zeroCrate = msw.db.crate.create({ name: 'test-zero-crate' });
msw.db.version.create({ crate: zeroCrate, num: '0.0.0' });

// Visit homepage
await page.goto('/');
await expect(page).toHaveURL('/');

// Should correctly display v0.0.0 for crates that actually have that version
await expect(page.locator('[data-test-new-crates] [data-test-crate-link]')).toContainText('test-zero-crate v0.0.0');

// Click on the crate to navigate to its details page
await page.click('[data-test-new-crates] [data-test-crate-link]');

// Verify we're on the crate details page
await expect(page).toHaveURL('/crates/test-zero-crate');

await page.goto('/'); // Re-visit to simulate the back navigation

// Should still display v0.0.0 correctly (this is the intended behavior)
await expect(page.locator('[data-test-new-crates] [data-test-crate-link]')).toContainText('test-zero-crate v0.0.0');
});
});
66 changes: 66 additions & 0 deletions tests/bugs/11772-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { click, currentURL, visit } from '@ember/test-helpers';
import { module, test } from 'qunit';

import { setupApplicationTest } from 'crates-io/tests/helpers';

module('Bug #11772', function (hooks) {
setupApplicationTest(hooks);

function prepare(context) {
let { db } = context;

// Create a crate that will appear in "New Crates" section
let newCrate = db.crate.create({ name: 'test-crate' });
db.version.create({ crate: newCrate, num: '1.2.3' });
}

test('crate versions should remain correct after navigating back from crate details', async function (assert) {
prepare(this);

// Visit homepage
await visit('/');
assert.strictEqual(currentURL(), '/');

// Verify initial correct version displays
assert.dom('[data-test-new-crates] [data-test-crate-link]').containsText('test-crate v1.2.3');
assert.dom('[data-test-just-updated] [data-test-crate-link]').containsText('test-crate v1.2.3');

// Click on a crate to navigate to its details page
await click('[data-test-new-crates] [data-test-crate-link]');

// Verify we're on the crate details page
assert.strictEqual(currentURL(), '/crates/test-crate');

await visit('/'); // Re-visit to simulate the back navigation

// Versions should still be displayed correctly, not v0.0.0
assert.dom('[data-test-new-crates] [data-test-crate-link]').containsText('test-crate v1.2.3');
assert.dom('[data-test-just-updated] [data-test-crate-link]').containsText('test-crate v1.2.3');
});

test('crates with actual v0.0.0 versions should display correctly', async function (assert) {
let { db } = this;

// Create a crate with an actual v0.0.0 version
let zeroCrate = db.crate.create({ name: 'test-zero-crate' });
db.version.create({ crate: zeroCrate, num: '0.0.0' });

// Visit homepage
await visit('/');
assert.strictEqual(currentURL(), '/');

// Should correctly display v0.0.0 for crates that actually have that version
assert.dom('[data-test-new-crates] [data-test-crate-link]').containsText('test-zero-crate v0.0.0');

// Click on the crate to navigate to its details page
await click('[data-test-new-crates] [data-test-crate-link]');

// Verify we're on the crate details page
assert.strictEqual(currentURL(), '/crates/test-zero-crate');

await visit('/'); // Re-visit to simulate the back navigation

// Should still display v0.0.0 correctly (this is the intended behavior)
assert.dom('[data-test-new-crates] [data-test-crate-link]').containsText('test-zero-crate v0.0.0');
});
});
Loading