Can I upgrade to an SSD with the newest macOS from an old HDD with old macOS on MacBook Pro 2012?

Broadly speaking, any volume with MacOS on it should boot on any Mac that the OS supports.

There are some exceptions to that rule, of course. The newer T2-chip Macs complicate things with their SecureBoot settings. And some Macs require firmware updates on their internal drive before they can use APFS volumes -- though in your case the EFI partition will be created when you install the OS.

So it should work.

The better alternative would be to create an 'installer disk' on a small USB thumb drive, and boot to that, on the 2012 MBP, to install on the SSD once fitted internally.

https://support.apple.com/en-gb/HT201372